Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求大牛看看我哪里错了 为什么wa?

Posted by yeshiwei at 2008-11-30 10:23:22 on Problem 1010
#include<iostream>
#include<algorithm>
using namespace std;

int stamp[26];
int req;
int l;
int ans[4],temp[4],p[4];//p[i]存temp[i]的类型
int astep,adif;//astep ans的长度,adif 是ans里邮票 的类型数
bool found,tie;
int cmpans(int *a,int la,int *b,int lb){//比较两个 结果的优劣。
	int i,j;
	for(i=la,j=lb;i>=0&&j>=0;i--,j--)
	{
		if(a[i]<b[j])return -1;
		if(a[i]>b[j])return 1;

	}
	if(i>0)return -1;
	if(j>0)return 1;
	return 0;
}
bool dfs(int r,int step,int pos,int dif)//r 当前剩余钱,step 当前邮票数目,
//pos邮票类型位置,dif 邮票类型数
{
	if(step>4)return false;
	if(r==0){	
		if(!found){
			memcpy(ans,temp,sizeof(ans));
			astep=step;
			adif=dif;
			found=true;
			tie=false;
		}
		else{
			if(dif<adif)return true;
			if(dif>adif)
			{
				memcpy(ans,temp,sizeof(ans));
				astep=step;
				adif=dif;
				tie=false;
				return true;
			}
			if(cmpans(ans,astep-1,temp,step-1)==0)
			{
				tie=true;
				return true;
			}
			if(cmpans(ans,astep-1,temp,step-1)>0){
				return true;
			}
			memcpy(ans,temp,sizeof(ans));
			astep=step;				
			tie=false;
			return true;

		}
	}
	bool ret=false;
	if(pos+1<l){
		if(dfs(r,step,pos+1,dif))ret=true;
	}
	if(r>=stamp[pos])
	{
		if(step>=1)
		{
			if(pos==p[step-1])
			{
				temp[step]=stamp[pos];
				p[step]=pos;
				if(dfs(r-stamp[pos],step+1,pos,dif))ret=true;
			}
			else
					{
				temp[step]=stamp[pos];
				p[step]=pos;
				if(dfs(r-stamp[pos],step+1,pos,dif+1))ret=true;
			}
		
		}
		else	{
				temp[step]=stamp[pos];
					p[step]=pos;
				if(dfs(r-stamp[pos],step+1,pos,dif+1))ret=true;
			}
		
	}
return ret;
}
void display(){
	if(!found){
	printf("%d ---- none\n",req);
	return;
	}
	if(tie){
	printf("%d (%d): tie\n",req,adif);
	return ;
	}
printf("%d (%d):",req,adif);
for(int i=0;i<astep;i++)
{
printf(" %d",ans[i]);
}
printf("\n");

}
int main()
{
		while(scanf("%d",&stamp[l=0])==1)
	{
		while(stamp[l]!=0)
		{
			scanf("%d",&stamp[++l]);
		}
		sort(stamp,stamp+l);
	
		while(scanf("%d",&req)==1&&req){
				found=false;
         dfs(req,0,0,0);
display();	
		}

	}


	return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator