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.
InputThe 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.
OutputFor 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
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
这道题其实并不是很难,但是有时候有几处小细节很容易困扰到做题人到思维,有一度时间我的输出中只要该元素存在相同的值,那么存在该元素的式子中就会在输出中有一个相同的式子,后来一遍遍的读了程序才发现是有一处忘记了return造成的。
这道题的思路就是对每个元素进行DFS深度优先搜索,是同时双重的一个搜索,就是有一步是一个个的往下搜,另外还有一条路线是搜下个不相同的元素(甚至可以是下下个、......以此类推),那么来讲一下我的主要结构吧:
void dfs(int b_i, int a_i, int sum)
{
if(sum==t)
{
flag=true;
for(int i=0; i<b_i-1; i++)
{
cout<<b[i]<<"+";
}
cout<<b[b_i-1]<<endl;
return;
}
if(sum>t) return;
if(a_i>=n) return;
b[b_i]=a[a_i];
dfs(b_i+1, a_i+1, sum+b[b_i]);
while(a_i+1<n&&a[a_i+1]==a[a_i])
{
a_i++;
}
dfs(b_i, a_i+1, sum);
}
这么一来就清晰明了了,我是对每个本状态中的b[i](也就是所需要的答案)进行操作,也可以作为充当记忆的数组,我们对b[i]进行一步步往下搜索的操作,让每个b[i]可以选择该元素也可以跳过这个元素去获得下一个不同的元素。
完整代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int t,n;
int a[20]; //输入值
int b[20]; //输出值
bool flag=false; //存在这样的答案吗?
bool cmp(int r1,int r2)
{
return r1>r2;
}
void dfs(int b_i, int a_i, int sum)
{
if(sum==t)
{
flag=true;
for(int i=0; i<b_i-1; i++)
{
cout<<b[i]<<"+";
}
cout<<b[b_i-1]<<endl;
return;
}
if(sum>t) return;
if(a_i>=n) return;
b[b_i]=a[a_i];
dfs(b_i+1, a_i+1, sum+b[b_i]);
while(a_i+1<n&&a[a_i+1]==a[a_i])
{
a_i++;
}
dfs(b_i, a_i+1, sum);
}
int main()
{
while(scanf("%d%d",&t,&n),!(t==0&&n==0))
{
flag=false;
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
cout<<"Sums of "<<t<<":"<<endl;
sort(a, a+n, cmp);
dfs(0, 0, 0);
if(!flag)
{
cout<<"NONE"<<endl;
}
}
return 0;
}