您的位置:首页 > 其它

Sum It Up hdu 1258

2018-03-30 11:12 369 查看

Sum It Up

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7345 Accepted Submission(s): 3874

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

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

这道题就是在 nyoj 1058 部分和问题上加上一个判断是否相等的函数

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node{
int len;//数组长度
int a[15];//数组
}nnum[1000];//结构体用来存储已经输出的没有重复的结果

int num[15],vis[15],ans[15];//输入数据 ,标记数组,记录满足sum=t的结果

int t,n,flag;// flag用来判断是否有满足sum=t的结果,以及记录当前已经存入结构体数组的个数

int judge_same(int a[],int k)//判断当前结果是否与之前重复
{
int r=0,i,j;
for(i=1;i<=flag;i++)//flag为当前已经存入结构体数组的个数
{
if(k!=nnum[i].len)//当前数组的长度与nnum[i].len不相等
continue;
for(j=0;j<nnum[i].len;j++)
if(nnum[i].a[j]!=a[j])
break;
if(j==nnum[i].len)//完全相等,改变r=1,结束循环
{
r=1;
break;
}
}
return r;
}

void dfs(int m,int sum)
{
if(sum>t)
return ;
if(sum==t)
{
int k=0;
for(int i=n-1;i>=0;i--)
if(vis[i])
ans[k++]=num[i];
if(!judge_same(ans,k))//当前结果不与之前重复
{   flag++;
for(int i=0;i<k-1;i++)//输出
{
printf("%d+",ans[i]);
nnum[flag].a[i]=ans[i];//将当前结果存入结构体
}
printf("%d\n",ans[k-1]);
nnum[flag].a[k-1]=ans[k-1];//将当前结果存入结构体
nnum[flag].len=k;//将当前结果存入结构体

}
return ;
}
for(int i=m;i>=0;i--)
{
if(!vis[i])
{
vis[i]=1;
sum+=num[i];
dfs(i-1,sum);//注意这里是i-1,而不是m-1
vis[i]=0;
sum-=num[i];
}
}
}

int main()
{
while(scanf("%d %d",&t,&n) && (t+n))
{
memset(num,0,sizeof(num));
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
sort(num,num+n);//升序排序

flag=0;
printf("Sums of %d:\n",t);
dfs(n-1,0);//深搜
if(!flag)//没有符合条件的结果
printf("NONE\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDU 搜索 hdu1258 acm