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 ...
随机推荐
- 一键切换皮肤的解决思想及iframe嵌套时寻找下级iframe的方法
项目中有个一键切换皮肤的功能,感觉还不错,记录下,就是各颜色样式设置起来太复杂了,不知道有没有更简便的方法: 1.切换皮肤结构层 <li title="<s:text name= ...
- c++call back
#include "stdafx.h" struct A; typedef void(A::*MemFuncPtr) (int* e); class A { int a; }; c ...
- JDBC具体解释(2)
1.载入驱动程序. 注冊驱动程序有多方法,Class.forName();是一种显式地载入.当一个驱动程序类被Classloader装载后,在溶解的过程中,DriverManager会注冊这个驱动类的 ...
- Android -- Camera聚焦流程
Camera.java autoFocus()聚焦回调函数 @Override public void autoFocus() { //记录当前聚焦开始时间 mFocusStartTime = Sys ...
- qt creator修改程序编码(解决中文乱码问题)的方法
qt creator修改程序编码(解决中文乱码问题)的方法 qt creator修改程序编码的功能有几处. 1.edit - select encoding 选择载入(显示)编码和储存编码,其中GB2 ...
- SQL-查询排名
select row_number() over(order by amount) as rank,* from dbo.t_group
- java 反射获取属性值 方法
public static void main(String[] args) throws SecurityException, ClassNotFoundException, IllegalArgu ...
- MVC 之 属性详解
一.System [AttributeUsage]:指定另一特性类的用法.无法继承此类. [CLSCompliant]:指示程序元素是否符合公共语言规范 (CLS).无法继承此类. [ContextS ...
- error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
G:\Soft\python\word_cloud-master>python setup.py install error: Microsoft Visual C++ 9.0 is requi ...
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
AsyncHttpClient开源框架android-async-http还是非常方便的. AsyncHttpClient该类通经常使用在android应用程序中创建异步GET, POST, PUT和 ...