博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
搜索(DFS)
阅读量:5273 次
发布时间:2019-06-14

本文共 2422 字,大约阅读时间需要 8 分钟。

Sum It Up
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5684   Accepted: 2894

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 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 15 3 2 1 1400 12 50 50 50 50 50 50 25 25 25 25 25 250 0

Sample Output

Sums of 4:43+12+22+1+1Sums of 5:NONESums of 400:50+50+50+50+50+50+25+25+25+2550+50+50+50+50+25+25+25+25+25+25
#include 
#include
#include
#include
#include
using namespace std;const int maxn=10+5;int a[maxn],cur[maxn];int ok,sum,n;inline void DFS(int index,int cursum,int cnt){ if(cursum==0) { ok=0; printf("%d",cur[0]); for(int i=1;i

 

转载于:https://www.cnblogs.com/scottding/p/3712977.html

你可能感兴趣的文章
C#设置程序自启动
查看>>
Hadoop基准测试(一)
查看>>
Linux下解压缩文件命令总结
查看>>
通过cookie验证用户登录
查看>>
js-数组和字符串转化
查看>>
客户端链接如何判断Socket的实时连接
查看>>
读书笔记十四:TCP/IP详解之TCP的成块数据流
查看>>
print语句中逗号(,)和反斜杠(\)的区别
查看>>
contentType
查看>>
Java中HashMap和TreeMap的区别深入理解
查看>>
【2012年终总结】之四 获得CPU信息,剪贴板
查看>>
【转载】c++内存布局
查看>>
深入理解JVM--类的执行机制
查看>>
从Oracle向PPAS移行不成功时的处理
查看>>
Oracle 学习第一篇
查看>>
node js 库的安装
查看>>
[LeetCode] Climbing Stairs
查看>>
Python下探究随机数的产生原理和算法
查看>>
Codevs 2503 失恋28天-缝补礼物
查看>>
在window把项目上传到github
查看>>