题目链接:

http://poj.org/problem?id=1564

题目:

Sum It Up
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5839   Accepted: 2984

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 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

这个题目是典型的dfs。。

我认为基本的就是反复的数字不须要进行搜索了。由于已经搜索过了。否则会反复。。

当不满足条件时返回上一臣调用处。。

所以代码为:

#include<cstdio>
#include<cstdlib>
const int maxn=100+10;
int a[maxn],b[maxn];
int t,n,ok;
void dfs(int i,int j,int sum)
{
int k;
if(sum>t)
return;
if(sum==t)
{
printf("%d",b[1]);
for(k=2;k<j;k++)
printf("+%d",b[k]);
printf("\n");
ok=1;
return;
}
for(k=i;k<=n;k++)
{
b[j]=a[k];
dfs(k+1,j+1,sum+a[k]);
while(a[k]==a[k+1])
k++;
}
} int main()
{
int sum;
while(scanf("%d%d",&t,&n)!=EOF)
{
if(t==0&&n==0) return 0;
sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("Sums of %d:\n",t);
ok=0;
if(sum<t)
{
printf("NONE\n");
continue;
}
else
dfs(1,1,0);
if(!ok)
printf("NONE\n");
}
return 0;
}

poj1564 Sum it up的更多相关文章

  1. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

  2. poj1564 Sum It Up (zoj 1711 hdu 1258) DFS

    POJhttp://poj.org/problem?id=1564 ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=711 ...

  3. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  7. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  8. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

随机推荐

  1. window.open()页面之间函数传值

    项目中遇到的问题,使用window.open()开一个页面之后,cookie会消失,所以无法一键切肤不管作用,解决方案如下: window.open()总结: window.open("sU ...

  2. Transformer中引用iqd作为数据源的时提示TR1008无法连接问题

    刚接触tr的同行们可能都会遇到这么一个问题,当然我也遇到了. 今天把问题的解决办法详细的说明一下 从问题的表面来看是从cognos.ini文件缺少了什么配置东西,但是cognos10的安装中没有cog ...

  3. (转)NGUI研究院之三种方式监听NGUI的事件方法

    NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直接绑定在按钮上,当按钮点击时就可以监听到,这种方法不太好很不 ...

  4. win10 右键发送到 目录

    win10 右键发送到 目录 C:\Users\llskj\AppData\Roaming\Microsoft\Windows\SendTo C:\Users\llskj\AppData\Roamin ...

  5. UML建模学习1:UML统一建模语言简单介绍

    一什么是UML? Unified Modeling Language(UML又称为统一建模语言或标准建模语言)是国际对象管理组织OMG制定的一个通 用的.可视化建模语言标准.能够用来描写叙述(spec ...

  6. [LeetCode-20]Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  7. Jmeter-Maven-Plugin高级应用:Proxy Configuration

    Proxy Configuration Pages 12 Home Adding additional libraries to the classpath Advanced Configuratio ...

  8. 使用socket BPF/Linux内核工程导论——网络:Filter(LSF、BPF、eBPF)

    使用socket BPF linux 下的 包过滤器 BPF Linux内核工程导论——网络:Filter(LSF.BPF.eBPF) 注意(文中描述的内容): 此外,这段BPF代码还存在的一个问题是 ...

  9. linux查询文件中某几行

    查询文件中某几行: sudo cat /etc/tinyproxy.conf | head -n | tail -n + [一]从第3000行开始,显示1000行.即显示3000~3999行 cat ...

  10. 解决mysql下区分表名大小写的问题

    MySQL在Linux下采用 rpm方式安装后默认是: 数据库名与表名\表的别名\变量名是严格区分大小写 1.用root帐号登录,/etc/ mysql/my.cnf中的[mysqld]后添加lowe ...