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 file 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 x 1 , . . . , x n . 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 x 1 , . . . , x n 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 distinct; the same sum cannot 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
俺一天的时间都耗在这道题上了~~ T-T
第一种虽然过了样例,但是提交wa:
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
int num[15];
int SUM,n;
int vis2[15];
int vis[15];
int countn;
vector <int > p;
vector<int > ans2[100000000];
int T;
void dfs(int sum,int k,vector<int> &p)//k 计数,用来统计一组成功样例中元素个数
{
if (sum>SUM)
return;
if (sum==SUM)
{
ans2[countn].clear();
for (int i=0;i<k;i++)
{
ans2[countn].push_back(p[i]);
}
sort(ans2[countn].begin(),ans2[countn].end(),greater<int>());
countn++;
}
for (int i=0;i<n;i++)// 这个就是错误的根本,由于是用k值记录深度向后递归的,0开始查找产生了大量的重复数据,
//使在给出的样例稍微大一些的时候就会超内存
//正确的方法是,在每一次进入递归的时候,for 循环中i的赋值都应该是i+1 这样在一次递归中,
//就不会重复访问比当前元素靠前的元素了,即只能在当前元素后面的位置再查找,当然递归时
//记录深度的就不再是k了,重新声明一个变量(length)记录一组数据的长度
//只是这种方法还是要去重,因为后搜索到的一组数据还是可能会和以前搜到的相同~~~~~~~
{
if (num[i]==SUM)
continue;
if (!vis[i]&&sum<SUM)
{
vis[i]=1;
p.push_back(num[i]);
dfs(sum+num[i],k+1,p);
p.pop_back();
vis[i]=0;
}
}
}
int check(int i,int k)//检查是否相同 相同返回1 不相同返回0
{
if (ans2[i].size()!=ans2[k].size())
{
return 0;
}
for (int p=0;p<ans2[i].size();p++)
{
if (ans2[i][p]!=ans2[k][p])
return 0;
}
return 1;
}
void output()
{
// cout<<countn<<endl;
// memset(vis2,0,sizeof(vis2));
for (int i=0;i<countn;i++)
{
for (int j=i+1;j<countn;j++)
{
if (check(i,j)&&!vis[j])
{
vis[j]=1;
}
}
}
for (int i=0;i<countn;i++)
{
if (!vis[i])
{
for (int j=0;j<ans2[i].size();j++)
{
cout<<ans2[i][j];
if (j!=ans2[i].size()-1)
{
cout<<"+";
}
}
cout<<endl;
}
}
}
int main()
{
int he;
while (cin>>SUM>>n)
{
he=0;
T=0;
memset(vis,0,sizeof(vis));
memset(vis2,0,sizeof(vis2));
for (int i=0;i<n;i++)
{
cin>>num[i];
he+=num[i];
}
countn=0;
p.clear();
memset(ans2,0,sizeof(ans2));
if (he<SUM)
cout<<"NONE"<<endl;
else
{
dfs(0,0,p);
output();
}
}
return 0;
}
开始的时候并没有注意到搜索的判重,写完了dfs后无论怎么判重调试,都会出好多重复数据
这个基本上已经改的面目全非了~
ac—code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include<string.h>
using namespace std;
int num[15];
int visit[15];
int sum;
int ans[1000][15],p[13],len[1000];
int n,SUM,length,countn;
int flag;
void DFS(int sum,int d)
{
if ( sum == SUM )
{
flag = 1;
len[countn] = length;
memcpy( ans[countn], p, length*sizeof(int));
countn++;
}
for (int i=d; i<=n; i++)//d这步来继续向后寻找,且在一次搜索中不访问前面的数据
{
if ( !visit[i] && sum + num[i] <= SUM )
{
visit[i] = 1;
p[length] = num[i];
length ++;
DFS(sum+num[i],i+1);
visit[i] = 0;
length--;
}
}
}
int check( int i,int k)
{
if (len[i] != len[k])
return 0;
for (int j=0; j<len[i]; j++)
if ( ans[i][j] != ans[k][j] )
return 0;
return 1;
}
void output()
{
int vis2[1000];
memset(vis2,0,sizeof(vis2));
for (int i=0; i<countn; i++)
{
for (int k=i+1; k<countn; k++)
if ( check(i,k) && !vis2[k] )
{
vis2[k] = 1;
}
}
for (int i=0; i<countn; i++)
if ( !vis2[i])
{
cout << ans[i][0];
for (int k=1; k<len[i]; k++)
cout << '+'<< ans[i][k];
cout << endl;
}
}
int main()
{
while ( cin >> SUM >> n )
{
if (SUM==0&&n==0) break;
int he=0;
sum = 0;
length = 0;
flag = 0;
countn = 0;
for (int i=1; i<=n; i++)
{
cin >> num[i];
he+=num[i];
}
cout << "Sums of " << SUM << ":" << endl ;
if (he<SUM)
cout << "NONE" << endl;
else
{
memset( visit,0,sizeof(visit) );
memset( ans,0,sizeof(ans) );
DFS(0,1);
output();
if ( flag == 0 )
cout << "NONE" << endl;
}
}
return 0;
}
尼玛~