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

Re:谁能告诉我怎么样除去重复的

Posted by deathspeaker at 2009-05-20 20:02:04 on Problem 1564
In Reply To:谁能告诉我怎么样除去重复的 Posted by:6233843 at 2006-07-17 18:13:52
int ans[12],n,k,data[12],f;
void search(int i,int s){
	int j;
	if(!s){
		for(f=j=1,printf("%d",ans[0]);j<k;j++)printf("+%d",ans[j]);
		printf("\n");
		return;
	}
	if(i>=n||s<0)return;
	for(j=i;j<n;j++)if(j==i||data[j]!=data[j-1]){//这里的处理避免了重复的情况:data[j]!=data[j-1])
		ans[k++]=data[j];
		search(j+1,s-data[j]);
		k--;
	}
}
void main(){
	int j,t;
	while(scanf("%d%d",&t,&n),t){
		f=k=0;
		for(j=0;j<n;j++)scanf("%d",&data[j]);
		printf("Sums of %d:\n",t);
		search(0,t);
		if(!f)printf("NONE\n");
	}
}

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