hdu 1258
Sum It Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6448 Accepted Submission(s):
3365
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.
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.
'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.
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int num,n,pos;
int a[15],b[15];
bool judge = false;
void output(int depth)
{
for(int i =0 ;i< depth; ++i)
if(!i) printf("%d",b[i]);
else printf("+%d",b[i]);
printf("\n");
}
void dfs(int depth,int sum,int pos) //每一位数字只有两种可能(加/不加),pos表示当前进行到了哪一位,depth表示b里保存的数字数量便于打印
{
if(sum == num) {judge = true;output(depth); return;}
if(sum>num) return;// 超出了 终止递归
if(pos>=n) return; //选择的数的位置超出数据范围
b[depth] = a[pos];
dfs(depth+1,sum+a[pos],pos+1);
while(pos+1<n&&a[pos] == a[pos+1]) pos++;//关键 判重
dfs(depth,sum,pos+1);
}
int main()
{
while(scanf("%d%d",&num,&n) && num){
printf("Sums of %d:\n",num);
for(int i = 0; i<n; ++i) scanf("%d",&a[i]);
judge = false;
dfs(0,0,0);
if(judge == false) printf("NONE\n");
}
return 0;
}
hdu 1258的更多相关文章
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- HDU 1258 Sum It Up(dfs 巧妙去重)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...
- 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 ...
- hdu 1258 Sum It Up (dfs+路径记录)
pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1258 Sum It Up
Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- (step4.3.4)hdu 1258(Sum It Up——DFS)
题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...
- hdu 1258 DFS
I - 深搜 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bi ...
- HDU 1258 Sum It Up(DFS)
题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...
随机推荐
- 自己封装的ajax
/** * ITCAST WEB * Created by lsy on 2016/5/24. */ /* * 1. 请求的类型 type get post * 2. 请求地址 url * 3. 是异 ...
- INNODB引擎概述
INNODB存储引擎的历史概述: INNODB存储引擎是OLTP应用中核心表的首选存储引擎. INNODB存储引擎包含在所有mysql数据库的二进制发行版本中.早期其版本随着mysql数据库的更新而更 ...
- python中递归调用
递归一个通俗的解释就是,在函数中调用函数本身:伪代码如下: In [31]: def fun(): ....: fun() # 这个递归没有任何作用,只是为了说明什么是递归 递归(Recursion) ...
- xdebug安装方法
打开网址:https://xdebug.org/wizard.php 把phpinfo页面中输出的所有内容复制过来,粘贴在此处点下面那个按钮,系统会分析出你需要下载哪个版本的x-debug,还会告诉你 ...
- Git 基础 —— 安装 配置 别名 对象
Git 基础学习系列 Git 基础 -- 安装 配置 别名 对象 Git 基础 -- 常用命令 Git 基础 -- 常见使用场景 Git基础 -- Github 的使用 Git 安装 Git下载地址 ...
- 事务(Transaction)
1.演示转账的功能:(1)创建一张表示学生表表 CREATE TABLE student( id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(50), ac ...
- 【Coursera】Security Introduction -Eighth Week(2)
Review -Terminology(术语): Confidentiallity & Integrity 泄密 & 欺骗 Confidentiallity: Prevent unau ...
- 【Coursera】Seventh Week
Application Layer:Use the services of the TCP layer Quick Review Link Layer(Ethernet):gets the data ...
- MVC ---- 标准查询运算符
标准查询运算符:定义在System.Linq.Enumerable类中的50多个为IEnumerable<T>准备的扩展方法,这些方法用来 对它操作的集合进行查询筛选. 筛选集合Where ...
- 微信小程序之可滚动视图 scroll-view 的使用注意
微信小程序之可滚动视图 scroll-view 使用注意: 1.scroll-view 中的需要滑动的元素不可以用 float 浮动: 2.scroll-view 中的包裹需要滑动的元素的大盒子用 d ...