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

贴个代码,判重的方法也是跟别人那学来的,给大家点参考

Posted by ls03101409 at 2011-09-06 12:30:27 on Problem 1564
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
void dfs(int now,int num,int cur);
int t,n;
int flag;
int a[15];
int ans[15];
int main()
{
	while(cin>>t>>n)
	{
		if(n==0)break;
		int i;
		for(i=0;i<n;i++)cin>>a[i];
		flag=0;
		cout<<"Sums of "<<t<<":"<<endl;
		dfs(0,t,0);
		if(!flag)cout<<"NONE"<<endl;
	}
	return 0;
}
void dfs(int now,int num,int cur)
{
	if(num==0)
	{
		flag=1;
		cout<<ans[0];
		int i;
		for(i=1;i<cur;i++)
		{
			cout<<"+"<<ans[i];
		}
		cout<<endl;
		return;
	}
	else
	{
		int i;
		int pre=-1;
		for(i=now;i<n;i++)
		{
			if(num>=a[i]&&a[i]!=pre)
			{
				pre=a[i];
				ans[cur]=a[i];
				dfs(i+1,num-a[i],cur+1);
			}
		}
	}
}

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