Sum It Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7203 Accepted Submission(s): 3796
Problem Description
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3+1,2+2, and 2+1+1.(A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
Input
The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1,...,xn. If n=0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12(inclusive), and x1,...,xn will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.
Output
For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line 'NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distince; the same sum connot appear twice.
Sample Input
4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
Sample Output
Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25
【分析】DFS
题意:给定t和n 以及n个正数,从中选取一些使它们的和恰好为t。按格式要求,不重复输出所有的方案,且每种方案中的数降序排列。
关键:1°去重(加判断)
2°降序排列,选数时按降序序列从左到右选数,也就是说不能选了一个数a[i]再选数a[j](j<i)
3°剪枝:输入完成后求出n个数的和total,当total<=t时可直接作出判断,不必搜索;此外,搜索过程中若发现已选的数之和>t,则不再继续搜索,此方案不合题意。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn=15;
using namespace std;
int t,n;
int cnt,suc;
int a[maxn],vis[maxn];
int temp[maxn],ret[10005][maxn];
bool cmp(int a,int b)
{
return a>b;
}
int Judge(int *t,int r[][maxn],int ans,int cnt)
{
int i,j;
int flag=0;
if(cnt==0)
return 1;
else
{
for(i=0;i<cnt;i++)
{
flag=0;
for(j=0;j<ans;j++)
{
if(r[i][j]==t[j])
flag++;
}
if(flag==ans)
return 0;
}
return 1;
}
}
void dfs(int ans,int idx,int sum)
{
int i;
if(sum>t)
return;
else if(sum==t)
{
suc=1;
if(Judge(temp,ret,ans,cnt))
{
for(i=0;i<ans-1;i++)
{
printf("%d+",temp[i]);
ret[cnt][i]=temp[i];
}
printf("%d\n",temp[ans-1]);
ret[cnt++][ans-1]=temp[ans-1];
}
}
else
{
for(i=idx;i<n;i++)
{
if(!vis[i])
{
if(sum+a[i]>t)
continue;
vis[i]=1;
temp[ans]=a[i];
dfs(ans+1,i,sum+a[i]);
vis[i]=0;
}
}
}
}
int main()
{
int i,total;
while(scanf("%d %d",&t,&n)!=EOF)
{
if(n==0)
break;
total=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
total+=a[i];
}
printf("Sums of %d:\n",t);
if(total<t)
printf("NONE\n");
else if(total==t)
{
for(i=0;i<n-1;i++)
printf("%d+",a[i]);
printf("%d\n",a[n-1]);
}
else
{
sort(a,a+n,cmp);
memset(vis,0,sizeof(vis));
memset(ret,0,sizeof(ret));
cnt=suc=0;
dfs(0,0,0);
if(suc==0)
printf("NONE\n");
}
}
return 0;
}