题目链接:

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. 以太网帧、IP报文格式

    这几天完成一个对比以太网帧的程序(c语言),老师给了以太网帧头部和IP报文头部的结构体,跟实际抓取到的数据包的格式是相同的. 以太网帧头部的数据结构: typedef struct { unsigne ...

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

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

  3. #lspci | grep Eth

    该命令作用:将lspci的输出当做输入,从中找出包含Eth的行.在我的Fedora机器上运行结果为 [root@localhost etc]# lspci | grep Eth00:04.0 Ethe ...

  4. Activiti Designer 5.14.1插件安装和使用

    1.离线包下载 离线安装包下载:https://files.cnblogs.com/files/modou/Activiti_BPMN_2.0_designer.rar 2.安装 先把jars文件夹中 ...

  5. ZH奶酪:PHP解析URL及parse_url、parse_str、explode、implode函数说明

    首先看一下解析任意URL的代码: (1)获取协议类型:例如参考链接中的:http (2)获取主机地址:例如参考链接中的:my.oschina.net (3)获取当前页面在服务器的路径:例如参考链接中的 ...

  6. ASP.NET Page执行顺序如:OnPreInit()、OnInit()

    http://www.cnblogs.com/yeminglong/archive/2012/10/16/2725664.html 当页面进行回发时,如点击按钮,以上事件都会重新执行一次,这时的执行顺 ...

  7. js条件语句之职责链数组

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. python解析发往本机的数据包示例 (解析数据包)

    tcp.py # -*- coding: cp936 -*- import socket from struct import * from time import ctime,sleep from ...

  9. taro alias 的使用

    用来配置目录别名,从而方便书写代码引用路径.例如,使用相对路径书写文件引用如下: import A from '../../componnets/A' import Utils from '../.. ...

  10. python 读帧和绘图的区别

    capture = cv2.VideoCapture(0) while True: #img = cv.QueryFrame(capture) ret, frame = capture.read() ...