Sum It Up

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

Total Submission(s): 6720    Accepted Submission(s): 3535

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
 
Source
Problem : 1258 ( Sum It Up )     Judge Status : Accepted

RunId : 21150701    Language : G++    Author : hnustwanghe

Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>

using namespace std;

const int N = 50+5;
int x,n,a[N],save[N],pos;
bool flag;
bool cmp(const int x,const int y){
return x>y;
}
void DFS(int sum,int d){
if(sum>x) return;
if(sum==x){
flag = false;
for(int i=0;i<pos-1;i++)
printf("%d+",save[i]);
printf("%d\n",save[pos-1]);
return ;
}
int last = -1;
for(int i=d+1;i<=n;i++){
if(a[i]!=last){
save[pos++] = a[i];
last = a[i];
DFS(sum+a[i],i);
pos--;
}
}
}
int main(){
while(scanf("%d %d",&x,&n)==2 &&(x||n)){
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1,cmp);
flag = true;
printf("Sums of %d:\n",x);
DFS(0,0);
if(flag) printf("NONE\n");
}
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>

using namespace
std; const int N = 50+5;
int
x,n,a[N],save[N],pos;
bool
flag;
bool
cmp(const int x,const int y){
return
x>y;
}
void
DFS(int sum,int d){
if(
sum>x) return;
if(
sum==x){
flag = false;
for(int
i=0;i<pos-1;i++)
printf("%d+",save[i]);
printf("%d\n",save[pos-1]);
return ;
}
int
last = -1;
for(int
i=d+1;i<=n;i++){
if(
a[i]!=last){
save[pos++] = a[i];
last = a[i];
DFS(sum+a[i],i);
pos--;
}
}
}
int main(){
while(
scanf("%d %d",&x,&n)==2 &&(x||n)){
for(int
i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1,cmp);
flag = true;
printf("Sums of %d:\n",x);
DFS(0,0);
if(
flag) printf("NONE\n");
}
}

最新文章

  1. matlab画图函数plot()/set/legend
  2. LINQ系列:LINQ to SQL Where条件
  3. Ajax Step By Step5
  4. 一个数如果恰好等于它的因子之和,这个数就称为 &quot;完数 &quot;。例如6=1+2+3.编程&#160;&#160;&#160;&#160; 找出1000以内的所有完数。
  5. Web应用开发工具及语言需要具备的功能探索
  6. C#创建UTF8无BOM文本文件
  7. (转)iOS7界面设计规范(1) - UI基础 - 为iOS7而设计
  8. js单例模式
  9. mysql 增量导入到elasticsearch
  10. 【G-BLASTN 1.0正式发布】
  11. java8之lambda表达式入门
  12. Mahout安装部署
  13. 关于aop的两种方式-基于注解和基于aspectj
  14. 拾人牙慧篇之——linux文件挂载,基于nfs的文件共享系统安装配置
  15. linux 查看系统资源命令
  16. js 判断数组中是否有某值
  17. Stm32串口通信(USART)
  18. (4.28)for xml path 在合并拆分上的作用演示
  19. 厉害了,他用PS不是P照片而是……
  20. 1089. Insert or Merge (25)-判断插入排序还是归并排序

热门文章

  1. MYSQL安装失败,一打开就出现MySQL-Workbench已停止工作
  2. BZOJ 4373: 算术天才⑨与等差数列 线段树
  3. ex1.c 补
  4. 在xml文件中使用该控件
  5. iOS 自定义一对UI表现相反的按钮
  6. sklearn—无监督最近邻
  7. 一个蒟蒻的解题过程记录——洛谷P1003 铺地毯
  8. 四十二、python中异常
  9. MS入门学习笔记
  10. SpringBoot -- 配置mysql、hibernate