poj1564 Sum it up
题目链接:
http://poj.org/problem?id=1564
题目:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5839 | Accepted: 2984 |
Description
(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.
Input
positive integer less than 1000, n will be an integer between 1 and 12 (inclusive), and x 1 , . . . , x n 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.
Output
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 distinct; the same sum cannot appear twice.
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
Source
这个题目是典型的dfs。。
我认为基本的就是反复的数字不须要进行搜索了。由于已经搜索过了。否则会反复。。
当不满足条件时返回上一臣调用处。。
所以代码为:
#include<cstdio>
#include<cstdlib>
const int maxn=100+10;
int a[maxn],b[maxn];
int t,n,ok;
void dfs(int i,int j,int sum)
{
int k;
if(sum>t)
return;
if(sum==t)
{
printf("%d",b[1]);
for(k=2;k<j;k++)
printf("+%d",b[k]);
printf("\n");
ok=1;
return;
}
for(k=i;k<=n;k++)
{
b[j]=a[k];
dfs(k+1,j+1,sum+a[k]);
while(a[k]==a[k+1])
k++;
}
} int main()
{
int sum;
while(scanf("%d%d",&t,&n)!=EOF)
{
if(t==0&&n==0) return 0;
sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("Sums of %d:\n",t);
ok=0;
if(sum<t)
{
printf("NONE\n");
continue;
}
else
dfs(1,1,0);
if(!ok)
printf("NONE\n");
}
return 0;
}
poj1564 Sum it up的更多相关文章
- poj1564 Sum It Up dfs水题
题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...
- 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 ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
随机推荐
- 使用jQuery在上传图片之前实现缩略图预览
使用jQuery在上传图片之前实现缩略图预览 jQuery代码 01 $("#uploadImage").on("change", function(){ 02 ...
- C++设计模式实现--模板(Template)模式
一. 问题 在面向对象系统的分析与设计过程中常常会遇到这样一种情况:对于某一个业务逻辑(算法实现)在不同的对象中有不同的细节实现,可是逻辑(算法)的框架(或通用的应用算法)是同样的.Template提 ...
- (step4.1.2)hdu 1969(Pie——二分查找)
题目大意:n块馅饼分给m+1个人,每个人的馅饼必须是整块的,不能拼接,求最大的. 解题思路: 1)用总饼的体积除以总人数,得到每个人最大可以得到的V.但是每个人手中不能有两片或多片拼成的一块饼. 代码 ...
- 关于COM组件log的位置
进程内组件写的log,如果不指定路径直接提供文件名,log文件的位置在dll所在的目录中. 进程外组件写的log,如果不指定路径直接提供文件名,log文件的位置不在exe所在的目录中,而是在系统目录. ...
- Matlab实现:图像边缘提取
1. 边缘提取算法 方法一:一阶微分算子 Sobel算子 Sobel算子检测方法对灰度渐变和噪声较多的图像处理效果较好,Sobel算子对边缘定位不是很准确,图像的边缘不止一个像素. Roberts算子 ...
- 免费素材-Helium (AI, EPS, SVG, Icon Font)
在线演示 在线演示 本地下载 我很高兴和大家分享今天的素材-Helium.它有多种格式可供下载(AI, EPS, SVG, Icon Font) ,内容包含曲线.飞行器.上传下载.喇叭等类型.
- C#.NET常见问题(FAQ)-listView如何显示网格线
把GridLines设置为True 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的在线论坛: http:/ ...
- 使用Python脚本批量裁切栅格
对栅格的裁切,我们通常使用裁切(数据管理-栅格-栅格处理)或按掩膜提取(空间分析-提取分析)来裁切,裁切的矢量要素通常是一个要素图层或Shape文件.如果要进行批量处理,可以使用ToolBox中的批量 ...
- eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面
eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面,如果不想覆盖的话,可以这么设置: Window->Preferences->General->Se ...
- webpack打包过滤console.log
在webpack.prod.conf.js里面的plugins里面加上 drop_debugger: true, drop_console: true new webpack.optimize.Ugl ...