HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏
Sum It Up
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.
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.
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.
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
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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>;
#include<queue>
#include<algorithm>
using namespace std; int s[100];
int a[100];
int m,n,tot,cnt; bool cmp(int a,int b)
{
return a>b;
} void dfs(int x,int sum)
{
if(sum>n)
return;
if(sum==n)
{
cnt++;
for(int i=0;i<tot;i++)
{
if(i)
printf("+%d",s[i]);
else
printf("%d",s[i]);
}
printf("\n");
return;
}
for(int i=x+1;i<m;i++)
{ s[tot++]=a[i];
dfs(i,sum+a[i]);
tot--;
while(i+1<m && a[i] == a[i+1])
i++; } } int main()
{ while(~scanf("%d%d",&n,&m)&&(m||n))
{ for(int i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+m,cmp);
cnt=0;
printf("Sums of %d:\n",n);
dfs(-1,0);
if(!cnt)
printf("NONE\n");
}
return 0;
}
HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏的更多相关文章
- HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏
Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...
- HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏
FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...
- Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏
for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...
- Hdu1205 吃糖果 2017-06-29 14:26 24人阅读 评论(0) 收藏
吃糖果 Problem Description HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另 ...
- hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++ 分类: hdoj 2015-07-12 03:24 87人阅读 评论(0) 收藏
the algorithm of three version below is essentially the same, namely, Kadane's algorithm, which is o ...
随机推荐
- QQ消息无限发送!源代码
昨天我一个朋友发给我一个特别有趣的程序 可以无限发送QQ消息,非常有趣! 发送给朋友之后只要打开,便可自动发送消息. 点打开后 便可一直发送消息 用Edit plus 打开后 其源代码如下 是用VB ...
- express + mongodb 搭建一个简易网站 (五)
前面已经将导航中的“所有宝贝”页面连上了mongodb,现在我们就把其他的页面脸上数据库,将整个网站全部实现. 打开routes文件,找到jacket.js,将里面的代码修改如下: var expre ...
- 大型运输行业实战_day04_1_搭建ssm框架最容易犯的错误
错误1.MapperScannerConfigurer中应该去扫描包,而不是接口 如果出现上述错误,报错如下,注意我们在看报错日志的时候一点要从 后往前看 错误2.没有配置别名,又要使用别名 命名不规 ...
- The Hard Thing About Hard Things
1.大多数的管理书籍都是告诉你如何做正确的事,不把事情搞砸.而好书是告诉你,当事情已经搞砸时,你该怎么办. 2.这是个个真实的世界,他们偷走了你的梦想,可你却不知道是谁偷的.
- .net 委托的用法
定义了两个委托 //Func有返回值:Action无返回值.两个委托 Func<int,int> f= a =>a+1;//参数,返回值: int reslut=f(5);//6
- 第八章 高级搜索树 (b3)B-树:查找
- X_PU
通俗易懂告诉你CPU/GPU/TPU/NPU...XPU都是些什么鬼?[附把妹秘籍] 2017-10-27 19:54移动芯片/谷歌 作者:iot101君 物联网智库 原创 转载请注明来源和出处 现在 ...
- inner join和outer join
内连接 只连接匹配的行 左外连接 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行 A left join B等价于A left ...
- ASP.NET使用ListView数据绑定控件和DataPager实现数据分页显示(二)
使用ListView控件进行修改,删除与添加操作1.页面代码: <asp:ListView ID="lv2" runat="server" onpagep ...
- python作业(第十一周)基于RabbitMQ rpc实现的主机管理
作业需求: 可以对指定机器异步的执行多个命令 例子: >>:run "df -h" --hosts 192.168.3.55 10.4.3.4 task id: 453 ...