HDOJ1258 又是一道深搜入门题

HDOJ.1342 Lotto (DFS) Lotto点我挑战题目题意分析给出k(6 < k < 13)个数字,要求从这k个数字中选出升序的6个数字,并且按照字典序输出全部的可能,给出的k个数字已经按照升序排列好。 乍一看以为是排列组合,怎么想也想不到是用dfs来解决。按照这个数字选或者不选的逻辑,以为是dp什么的。最后看了题解才知道用dfs的方法做,也算是长见识了。作为dfs的第一道题,好好写,纪念一下。 dfs一般采用递归写法,或许是 阅读详情

Sum It Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6788    Accepted Submission(s): 3566


Problem Description
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.
 

Input
The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1,...,xn. If n=0 it signals the end of the input; otherwise, 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.
 

Output
For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line 'NONE'. The numbers within each sum must appear in nonincreasing order. 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.
 

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
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1312  1181  1175  1045  1195 


思路就是递归深搜,注意排重,因为题目要求不能输出重复的解
代码如下:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;

#define maxn 20
int n,t,a[maxn];
int s[maxn],flag;

void dfs(int x,int sum,int cnt){
    int i;
    if(sum>t) return;
    if(sum==t){
        for( i=0;i<cnt-1;i++) printf("%d+",s[i]);
        printf("%d\n",s[i]);
        flag=1;
    }
    else{
        int tmp=-1; //保留当前的数,排重
        for(int i=x;i<=n;i++){
            if(tmp!=a[i])  {
                tmp=s[cnt++]=a[i];
                dfs(i+1,sum+a[i],cnt);
                cnt--;
            }
        }
    }
}


int main(){
    while(cin>>t>>n && n && t){
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        printf("Sums of %d:\n",t);
        flag=0;
        dfs(1,0,0);
        if(flag==0) printf("NONE\n");
    }
    return 0;
}


sum it up ! 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. 立即下载

相关推荐

SECS/GEM协议库及开源代码

以下是在开发SECS/GEM协议过程中,在网络上搜集协议实现资源,希望对SECS/GEM协议感兴趣的同学能有所帮助。 开源代码 freesecs FREESECS 是一个开源的SEMI SECS协议库,采用C++语言实现,可运行于Linux, MacOS X, NetBSD。 实现协议:E37.1、E5 开源地址:freesecs download | SourceForge.net 下载地址:https://download.csdn.net/download/BT_LEO/24305429

码农的历程 9965

hdoj1258

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1838    Accepted Submission(s): 920 Problem Description Given a specified

◆◇丶生如夏花。谁来订阅我的悲伤 649

Fortran基础编程(3)——格式化输出

本篇推文将进一步讲解Fortran基础语法中格式化输出与声明部分,让我们的代码更加美观起来吧~

qq_45715554的博客 3830

Hdu 1258 Sum It Up :DFS 去重

Sum It Up 题目描述: 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

Fluence_YHL 334

基于立创·天猛星MSPM0G3507的简易PID电机控制入门项目实战

本文详细介绍了基于立创·天猛星MSPM0G3507开发板的简易PID电机控制入门项目实战。从硬件选型、电路设计、PCB布局,到软件编程中的编码器读取、PID算法实现与调参,手把手教你完成一个带屏幕显示的电机定速与定距控制项目,是学习嵌入式开发和经典PID控制算法的绝佳实践。

weixin_42596011的博客 513

天龙八部源码深度解析:从宠物系统到副本机制(C++游戏开发实战)

本文深度解析《天龙八部》源码架构,重点剖析宠物系统状态机实现与副本动态加载机制等核心模块。通过C++代码示例展示宠物AI状态迁移逻辑和副本场景管理技术,为现代游戏开发提供兼容性改造方案与设计启示。适合游戏开发者学习经典MMORPG的工程化实现路径。

weixin_29011239的博客 403

1.29题目翻译

链接:https://acs.jxnu.edu.cn/problem/HDU1250 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

weixin_61393233的博客 252

《Python基础教程》内容总览篇(持续更新中)

大家好,我是爱编程的喵喵。双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。喜欢通过博客创作的方式对所学的知识进行总结与归纳,不仅形成深入且独到的理解,而且能够帮助新手快速入门。个人精心开设的《Python基础课程》专栏订阅量接近900,帮助不少同学解决了Bug。

weixin_43178406的博客 35万+

第九章:致我们终将逝去的青春(深度优先搜索问题)

描述 /青 春 是 用 来 追 忆 的 /当 你 怀 揣 着 她 时 /她 一 文 不 值 /只 有 将 她 耗 尽 后 /再 回 过 头 看 /一 切 才 有 了 意 义 /爱 过 我 们 的 人 和 伤 害 过 我 们 的 人 /都 是 我 们 青 春 存 在 的 意 义 在ACM中找寻青春的意义: Given a specified total t and a list of n integers, find all distinct sums using numbers from the list

我的博客 334

OpenCV实例(一)人脸检测

计算机视觉使很多任务成为现实,其中两项任务就是人脸检测(在图像中定位人脸)和人脸识别(将人脸识别为特定的人)。OpenCV实现了一些人脸检测和识别的算法。从安全到娱乐,这些技术在现实环境中都有应用。介绍OpenCV的一些人脸检测和识别功能,并定义特定类型的可跟踪物体的数据文件。具体来说,将研究Haar级联分类器,通过分析相邻图像区域之间的对比度,确定给定图像或子图像是否与已知类型匹配。

小幽余生不加糖 5027

DO-178C详解:机载软件开发、验证与适航标准指南

DO-178C将软件划分为五个等级(A到E),这些等级是基于失效状态对航空器安全的影响而定。

qq_41854911的博客 2862

POJ 1564 Sum It Up (DFS+剪枝)

Sum It Up Time Limit: 1000MS   Memory Limit: 10000K

code_or_code 1474

ARM CoreLink 系列 5.1.1 -- CI-700 System Address Map 】

所有的CHI 命令都包含一个 Source ID 和 Target ID, 其中 Source ID 可以来自于 RN Node, Target ID 可以来自于HN node或者其他 SN node. 其中 Target ID 是通过 SAM解析传入的地址得到的。对于 CI-700中的所有 RN Node 中的 SAM 多会进行配置,因为RN Node 访问的是整个地址空间,不同的地址空间会有不同的属性(I/O, DRAM),所以会对RN SAM 配置多个不同属性的区域。

CodingCos的博客 775

HDOJ(HDU).1258 Sum It Up (DFS)

每组数据给出要凑出的目标数字num和数字个数n,然后依次给出n个数字。要求从n个数字中选出若干个数字,是的数字之和为nun。**重复的组合只输出一次。** 和之前做过的选数字的题目类似,也可以采用DFS的思想来做。这道题与[ HDOJ.1342 Lotto [从零开始DFS(0)]](http://blog.csdn.net/pengwill97/article/details/54850852)及其的相似:每个数

Pengwill's Blog 955

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

码一波题单+HDOJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:

想飞的小菜鸡 2912

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

HDOJ-1258 加数和问题[深搜]

Sum It Up Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1820Accepted Submission(s): 911 Problem Description Given a specified total t a...

weixin_30600197的博客 240

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

HDOJ题目分类

HDOJ题目分类 2011-03-04 11:19 模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1

681

HDOJ(HDU).1015 Safecracker (DFS)

HDOJ(HDU).1015 Safecracker [从零开始DFS(2)]题意分析首先默认有个字母到数字的映射,A=1,B=2,C=3 …… Z=26,给出你一个目标数字target,然后给出一串字母,要求从这些字母中选取5个数字vwxyz,满足 v - w^2 + x^3 - y^4 + z^5 = target。可能有多组解,只输出字典序最大的那个。嗨呀一看到这道题,是否有种似曾相识的感觉

Pengwill's Blog 2209

HDOJ分类

HDOJ分类 基础题:1000、1001、1004、1005、1008、1012、1013、1014、1017、1019、1021、1028、1029、1032、1037、1040、1048、1056、1058、1061、1070、1076、1089、1090、1091、1092、1093、1094、1095、1096、1097、1098、1106...

xingoo 220

HDOJ(HDU).1241 Oil Deposits(DFS)

HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)]点我挑战题目从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] — DFS思想与框架/双重DFS HDOJ.1010 Tempter of the Bone [从零开始DFS(1)] —DFS四向搜索/奇偶剪枝 HDOJ(HDU).1015 Safecracker [从零开始

Pengwill's Blog 1237

(置顶)HDOJ题目分类

HDOJ题目分类: 模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 11

LzyRapX 1550

【HDOJ】1258 Sum It Up

典型的深搜,剪枝的时候需要跳过曾经搜索过的相同的数目,既满足nums[i]=nums[i-1]&&visit[i-1]==0,visit[i-1]==0可以说明该点已经测试过。 1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 1005 5 6 int ...

weixin_34198797的博客 112

HDOJ(HDU).1035 Robot Motion (DFS)

HDOJ(HDU).1015 Robot Motion [从零开始DFS(4)]点我挑战题目题意分析给出地图规模n * m, 给出入口坐标(0,y),遵循以下规则,求解机器人能否走出地图。若能,输出走出地图所需要的步数,若不能,输出进入循环前走的步数和循环的步数。规则: 若当前格子为N,则只能向上走,若为S向下走,E向右走,W向左走。我第一感觉是模拟题,因为对于每个格子状态是唯一的,只有1组解:要

Pengwill's Blog 1075
上一篇: HDOJ1242 基础宽搜题
下一篇: HDOJ1455 (DFS+剪枝优化)和 HDOJ1518可做类比
罗侯
博客等级 码龄10年 135粉丝 258原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值