HDU 1258 Sum It Up
Sum It Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3538 Accepted Submission(s):
1788
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.
0 0
while(a[i+1] == a[i]) i++;)。
#include <iostream>
#include <algorithm>
using namespace std;
int t,n;
int a[],b[];
bool flag;
bool cmp(int a,int b)
{
return a>b;
}
void dfs(int sum,int k,int j) //k代表要求得数组下标,j代表整个数组的下标
{
int i;
if (sum>t)
return;
if (sum == t)
{
cout<<b[];
for (i=;i<k;i++)
cout<<"+"<<b[i];
cout<<endl;
flag = false;
return ;
}
for(i=j;i<n;i++)
{
if(sum+a[i]>t)
continue;
b[k] = a[i];
dfs(sum+a[i],k+,i+);
while(a[i+] == a[i]) //剪纸的重要一步,找到下一个不相等的数
i++;
}
}
int main()
{
int i;
while(cin>>t>>n && t && n)
{
flag = true;
for(i=;i<n;i++)
cin>>a[i];
sort(a,a+n,cmp);
cout<<"Sums of "<<t<<":"<<endl;
dfs(,,);
if (flag)
cout<<"NONE"<<endl;
}
return ;
}
HDU 1258 Sum It Up的更多相关文章
- 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 巧妙去重)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...
- 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(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- (step4.3.4)hdu 1258(Sum It Up——DFS)
题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...
- HDU 1258 Sum It Up(DFS)
题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...
- HDU 1258 Sum It Up (DFS)
Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 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 ...
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...
随机推荐
- 基于FSIOT_A 实验平台SAM3S4B cortex-M3的温湿度采集
作者:卢老师,华清远见嵌入式学院讲师. DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器.它应用专用的数字模块采集技术和温湿度传感器技术,确保产品具有极高的可靠性与卓越的长期稳 ...
- PHP.ini文件读取不到
Configuration File (php.ini) Path /usr/local/php/lib Loaded Configuration File (none) Linux 把 dtruss ...
- DBLink创建 ORA-12154: TNS: 无法解析指定的连接标识符
因为对oracle不了解,这个问题可TM的搞了好久! 走的弯路: 1. 在客服端的PLSQL连接工具上折腾,而不是在服务器的PLSQL解决 2. 配置的tnsnames.org文件在环境变量path( ...
- KeyValue与KeyData与KeyCode区别(转)
KeyValue与KeyData与KeyCode区别(转) KeyPress事件 KeyPressEventArgs参数 Handled /是否处理过KeyPress事件 KeyChar //按下的键 ...
- 用脚本来简化iOS美术同学的工作
用脚本来简化iOS美术同学的工作 问题 我们知道,在 iOS 开发中,为了使我们的 app 能够同时支持 iPhone 的 Retina 屏幕和普通屏幕,美术同学需要对 UI 设计稿中的每个元素进行 ...
- java web(三) Tomcat虚拟目录映射方式
Tomact服务器虚拟目录的映射方式 web应用开发好后若想被外界访问,需要将web应用所在的目录交给web服务器管理,这个过程称为虚拟目录的映射. 方式一:在server.xml文件的host元素中 ...
- Scala命令设置JVM参数的规则
Scala下设置JVM参数简单分析 Scala 启动shell脚本,简化后的scala REPL 启动命令大致如下所示: java -Xmx256M -Xms32M \-Xbootclasspath/ ...
- linux中,常用的账号管理命令
创建新用户:adduser 用户名创建新用户并将其加入一个现有组中:adduser 用户名 -G 组名创建新用户并使其只属于该组:adduser 用户名 -g 组名创建用户密码:passwd 用户名创 ...
- C++静态成员和静态成员函数
一:静态数据成员: 类体中的数据成员的声明前加上static关键字,该数据成员就成为了该类的静态数据成员.和其他数据成员一样,静态数据成员也遵守public/protected/private访问规则 ...
- 使用xtrbackup 热备MySQL数据库 以及恢复和自动删除脚本
直接上脚本 热备(全备) #!/bin/bash user='root' passwd='123456' my_config='/etc/my.cnf' #mysql configure log=fu ...