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< ...
随机推荐
- wxPython学习
http://www.cnblogs.com/coderzh/archive/2008/11/23/1339310.html 一个简单的实例: #!/usr/bin/python import wx ...
- 深入浅出 - Android系统移植与平台开发(七)- 初识HAL
作者:唐老师,华清远见嵌入式学院讲师. 1. HAL的module与stub HAL(Hardware AbstractLayer)硬件抽象层是Google开发的Android系统里上层应用对底层硬件 ...
- Java_cookie 和session 的区别详解
这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪 ...
- linux下搭建svn代码库
1.安装svn客户端 2.创建svn代码库 1.安装svn客户端 1.1.使用命令安装 1)CentOS $ yum install subversion 2)ubuntu sudo apt-get ...
- osg实例介绍
osg实例介绍 转自:http://blog.csdn.net/yungis/article/list/1 [原]osgmotionblur例子 该例子演示了运动模糊的效果.一下内容是转自网上的:原理 ...
- The Bus Driver Problem
题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=90648#problem/G 题意: 给每位司机分配一个白天和晚上的行车路线, ...
- [LintCode] Coins in a Line II 一条线上的硬币之二
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- 关于android端的json传输
比较通用的传输方法 import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; impo ...
- 手机触屏触摸特效javascript-TouchSwipe(依赖于jquery库)中文说明
by 郑州seo on 2013 年 7 月 6 日 in jquery, 网站建设 with 6 Comments 最近需要做一个手机小门户网站,因为目前主流的手机都是安卓和苹果的,他们的浏览器内核 ...
- zendFream 中的用到了Ajax(其中有搜索)分页
最近在用ZendFreamwork开发一个后台,其中用到了分页,ZendFreamwork自带的分页挺好用的,可是我其中用到了Ajax的局部刷新,在加上一些搜索条件,所以分页有点无头绪了.下面我来介绍 ...