HDU1258 Sum it up
Sum it up
题意:给定一个数sum,和n个数,求sum可以由这n个数里面的那几个数的和表示。
Given a specified total t and a list of n integers, 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.
注意:输入,输出要求比较高,另外不能有重复。
递归的时候应该及时退出。
http://acm.hdu.edu.cn/showproblem.php?pid=1258
#include<cstdio>
#include<iostream>
int sum,n;
int flag=0;
int a[20],ans[20];
void dfs(int sums,int cut,int x)//sums代表当前的和,cut代表ans里的[1,cut),x代表a中的第x个数
{
if(sums==sum){
for(int i=1;i<cut;i++){
flag=1;
if(i==cut-1)
printf("%d\n",ans[i]);
else
printf("%d+",ans[i]);
}
return ;
}//如果结果sums==sum按格式输出ans 并且flag=1;
int t=-1;
for(int i=x;i<=n;i++){
if(t!=a[i]){
ans[cut]=a[i];
t=a[i];//避免重复
dfs(sums+a[i],cut+1,i+1);
}
}//
return ;//递归要有结束条件,不能是return;
}
int main ()
{
while(scanf("%d %d",&sum,&n),n||sum)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
flag=0;
printf("Sums of %d:\n",sum);
dfs(0,1,1);
if(flag==0)
printf("NONE\n");
}
return 0;
}
HDU1258 Sum it up的更多相关文章
- hdu1258 Sum It Up (DFS)
Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...
- 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 ...
- 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 ...
随机推荐
- Nimbus<二>storm启动nimbus源码分析-nimbus.clj
nimbus是storm集群的"控制器",是storm集群的重要组成部分.我们可以通用执行bin/storm nimbus >/dev/null 2>&1 &a ...
- .Net Core Session验证码
1.验证码帮助类 namespace IdeaCore.Services.Common { public class ValidateCodeService : IValidateCodeServic ...
- .Net Core 常见问题整理
1.安装时报0x80070490 找不到元素 这里应该是vs只装了web没有装c++ 下载一个 VC_redist.x64.exe 安装就行了 https://github.com/dotnet/co ...
- Openjudge-计算概论(A)-鸡尾酒疗法
描述: 鸡尾酒疗法,原指“高效抗逆转录病毒治疗”(HAART),由美籍华裔科学家何大一于1996年提出,是通过三种或三种以上的抗病毒药物联合使用来治疗艾 滋病.该疗法的应用可以减少单一用药产生的抗药性 ...
- Xcode8中添加SnapKit框架报错,编译失败
既然SnapKit的作者说SnapKit已经支持Swift3.0了,那么我们就先来适配SnapKit,首先用Xcode8新建一个空项目,利用Cocoapods导入SnapKit. Podfile 打 ...
- dbg调试
一.启动停止dbg dbg:tracer(). dbg:stop(). 二.跟踪常用函数 1.跟踪进程 dbg:p(PidSpec,TraceFlags). PidSpec Pid 一个特定进程ID ...
- VMWare网络链接三种方式
本文转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/03/15/1985084.html VMware虚拟机上网络连接(networ ...
- javaWEB总结(1):第一个servlet程序
1.新建一个javaWeb工程,工程的目录如下 2.在com.dao.chu的包下新建一个HelloServlet.java类 package com.dao.chu; import java.io. ...
- jQuery实现的简单分页功能的详细解析
分页功能在项目开发中不可或缺,老司机操作起来就和呼吸一样简单,新手恐怕就会吃力一些. 今天我回顾了一下具体的操作步骤,决定详细的分析一下每一步的实现目的及原理. 我们会创建一个简单的json文件来模拟 ...
- wmic应用实例
实例应用 1.磁盘管理 查看磁盘的属性 wmic logicaldisk list brief ::caption=标题.driveID=驱动器ID号.model=产品型号.Partitions=分区 ...