POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564
题目大意:给定一个整数t,和n个元素组成的集合。求能否用该集合中的元素和表示该整数,如果可以输出所有可行解。1<=n<=12
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 分析: 代码如下:
# include<cstdio>
# include<iostream>
# include<algorithm>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int a[],t,n;
bool flag;
int ans[],len; void dfs(int sum,int mark){
int i;
if(sum == ){
flag = true;
printf("%d",ans[]);
for(i=;i<len;i++)
printf("+%d",ans[i]);
printf("\n");
return;
}
if(sum< || mark>=n) return ;
for(i=mark; i<n; i++){
if(i==mark || a[i] != a[i-]){
ans[len++] = a[i];
dfs(sum-a[i],i+);
len--;
}
}
}
int main(){
int i;
while(scanf("%d%d",&t,&n) && n){
int temp = ;
for(i=;i<n;i++){
scanf("%d",&a[i]);
temp += a[i];
}
printf("Sums of %d:\n",t);
if(temp<t){
printf("NONE\n");
continue;
}
flag = false;
len = ;
sort(a,a+n,cmp);
dfs(t,);
if(!flag)
printf("NONE\n"); }
return ;
}
POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)的更多相关文章
- [poj 2331] Water pipe ID A*迭代加深搜索(dfs)
Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...
- POJ 1564 Sum It Up(DFS)
Sum It Up Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- 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 ...
- HDU 1024:Max Sum Plus Plus(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...
- Sum It Up---poj1564(dfs)
题目链接:http://poj.org/problem?id=1564 给出m个数,求出和为n的组合方式:并按从大到小的顺序输出: 简单的dfs但是看了代码才会: #include <cstdi ...
- HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏
Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...
- CodeForces 489C Given Length and Sum of Digits... (dfs)
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- HDU 2208 唉,可爱的小朋友(DFS)
唉,可爱的小朋友 Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)
Description Pattern lock security is generally used in Android handsets instead of a password. The p ...
随机推荐
- CPLEX IDE 菜单栏语言设置( 中文 英文 韩文 等多国语言 设置)
http://www-01.ibm.com/support/docview.wss?uid=swg21573032 Technote (FAQ) Question How to change the ...
- 解决Ubuntu下sublime3无法输入中文
参考site: https://github.com/YoungZHU/sublime-imfix 1. 下载sublime-imfix.c 假设下载到了 home(-)目录下 2. 安装c\C++ ...
- Android 2014年1月22日
一.广播优先顺序 Android广播有两个很重要的要素: 1 广播 - 用于发送广播 有序广播 - 被广播接收器接收后,可被终止,无法往下继续传达. 典型代表:短信广播 普通 ...
- Python的模块,模块的使用、安装,别名,作用域等概念
所谓的模块就是将不同功能的函数分别放到不同的文件中,这样不仅有利于函数的维护,也方便了函数的调用.在Python中,一个.py文件就是一个模块(Module). 在模块的上层有一个叫做包(Packag ...
- VirtualBox上Ubuntu 共享文件夹
1. virtualbox 菜单栏中设备-->共享文件夹,添加一个共享文件夹,比如共享文件夹路径是D:/share,共享文件夹名称是share. 2. 进入虚拟Ubuntu,在命令行终端输入: ...
- POJ 1325、ZOJ 1364、HDU 1150 Machine Schedule - from lanshui_Yang
Problem Description As we all know, machine scheduling is a very classical problem in computer scien ...
- JavaScript- 省市联动代码
以下是JS省市联动菜单代码: 代码一: <html> <head> <title></title> <script language=" ...
- Hive sql 语法解读
一. 创建表 在官方的wiki里,example是这种: Sql代码 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name d ...
- Codeforces 417E Square Table(随机算法)
题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...
- android79 Fragment生命周期
切换成01时依次调用onCreate,onStart,onResume方法,切换到03的时候01依次onPause,onStop,onDestroy,03依次onCreate,onStart,onRe ...