1133: 第九章:致我们终将逝去的青春

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 18  Solved: 12
[Submit][Status][Web Board]

Description

/青 春 是 用 来 追 忆 的

/当 你 怀 揣 着 她 时

/她 一 文 不 值

/只 有 将 她 耗 尽 后

/再 回 过 头 看

/一 切 才 有 了 意 义

/爱 过 我 们 的 人 和 伤 害 过 我 们 的 人

/都 是 我 们 青 春 存 在 的 意 义

在ACM中找寻 青春的意义:

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 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
【解析】
题目的意思就是就是给出我们数字的总和n,再给你m个数,然后叫你输出这n个数能组合成这个数的方案的总和.其实
这道题就算能想到是深搜..不过也还是有点懵,所以此处参考的网上代码。这个地方怎么样可以不出现重复的方案数
就算当我们遇到在某一层搜索的时候遇到了同样的数字我们就跳过。具体看代码解析吧。

#include<iostream>
#include<cstdio>
#include<cstring>
int n,flag,m;
int a[10001],b[10001];
using namespace std;
void dfs(int deep,int sum,int length)
{int i;if(sum>n)return;//表示数字的和比规定的数字大了返回上一层else if(sum==n){flag=1;for(i=0;i<length;i++){if(i==0)printf("%d",b[i]);//输出方案elseprintf("+%d",b[i]);}printf("\n");}for(i=deep;i<m;i++){b[length]=a[i];//把可以进行方案组合的数组一个一个的给它dfs(i+1,sum+a[i],length+1);//向下搜索,这里的总和是sum+a[i]是需要注意的while(a[i]==a[i+1])//遇到同样的数需要继续往下走i++;}
}
int main()
{int i;while(~scanf("%d",&n)){flag=0;memset(a,0,sizeof(a));memset(b,0,sizeof(b));scanf("%d",&m);if(n==0&&m==0)break;for(i=0;i<m;i++){scanf("%d",&a[i]);}printf("Sums of %d:\n",n);dfs(0,0,0);if(flag==0){printf("NONE\n");}}
return 0;
}

ZCMU-1133- 第九章:致我们终将逝去的青春相关推荐

  1. 绝望而沉重的爱·《致我们终将逝去的青春》

    陈孝正的母亲是辛夷坞作品<致我们终将逝去的青春>中的一个人物,对陈孝正影响非常大. 陈孝正的母亲对陈孝正的爱是如此沉重而绝望,看着都让人沉甸甸的.这份重量是一般人所不能承受的. 陈孝正还没 ...

  2. 致我们终将逝去的青春

    致我们终将逝去的青春 踩着素白的雾霭起步,晨曦的彩霞落寞地逼向幻想,沿着夕阳殊途的文脉,光阴碎落在记忆的轮廓里.让我们捡着星星的碎片,一起踏上这段追寻青春的记忆-- 冗长的雨季中,一个梦境重叠着另一个 ...

  3. 等候·《致我们终将逝去的青春》

    不知道你是不是会让你的恋人陷入与无穷无尽的等待,如果你成功的做到了这一点,恭喜你,那么你的男朋友或者女朋友就会随时离开你,这不是公平不公平的问题,而是你爱不爱的问题,如果真的爱,你不会让她失望,不会让 ...

  4. 《致我们终将逝去的青春》

    前两天看完了<致我们终将逝去的青春>这本书,有些关乎感情 关乎爱 关乎岁月的一些感触.    一个媒体评价:十八岁,郑微对阮阮说:"我是谁,我是天下无敌的玉面小飞龙,有什么我得不 ...

  5. 致我们终将逝去的青春第三季!

    岁月长河!滚滚而去!谁又能青史留名? 现在由百晓生为你记录64期的点点滴滴!你们准备好了吗!!go! 烟雨朦胧的日子飞来一条喜讯!60瓶汽水!兄弟们好喝吗?????感谢XX同志为我们在枯燥的学习生活带 ...

  6. 爱与灾难·《致我们终将逝去的青春》

    阮莞简直像是古书中走出来的美人,虽未见人,但是却给我留下深深的印象. 认真,严谨,知性,温柔,敦厚,喜爱看各种书,到了大学的时候,谈的是高中的男朋友赵世勇. 而阮莞对赵世勇对他的欺骗也能忍受,这一点不 ...

  7. 摘录与评论·《致我们终将逝去的青春》

    其实如果就上一篇,<青春是黄鹤>你说你没看爽,这只是因为,我总结出了博客观众的习惯,不喜长文,只能写在八百字就可以了结了,否则,长篇累牍,谁爱观看啊.因此拆分为两部分,上部分为<致我 ...

  8. 当痴女遇到渣男·《致我们终将逝去的青春》

    在辛夷坞的书中,郑微主动追的陈孝正,结果追到了,好像是在说,真的呀"女追男隔层纱".但是就从一个外人的角度来讲,这种组合真的不好,或者叫不搭调吧.细节如何,且听我一一到来. 作为从 ...

  9. 青春是黄鹤·《致我们终将逝去的青春》

    看了这本书,虽然陆陆续续写了几篇文章,但总是感觉有疏漏,更加上近来有一个比较大的书单,不允许自己将这些喜爱的讨厌的人物一一写去,于是写了这个总篇.这样这个<致我们终将逝去的青春>也算了结了 ...

最新文章

  1. LeetCode简单题之较大分组的位置
  2. UE4场景设计学习教程
  3. 电子学会青少年编程等级考试Python案例08
  4. jquery option 动态 selected
  5. [UWP]了解IValueConverter
  6. linux查看目录下文件个球,球服务翻译……只找到以前版本的。新版的服务名称改了……...
  7. 终于解决H3C交换机reset saved-configuration后不能启动的问题
  8. 封装一个方法,找出数组中重复数大于n的元素集合
  9. php - MySQL创建新用户并授权
  10. leetcode959. Regions Cut By Slashes
  11. The host '192.168.174.130' is unreachable. the host may be down..............
  12. 【详解】P问题、NP问题和NPC问题详解(千禧年问题之首)
  13. 行政区域村级划分数据库_最新行政区划省市区街道乡镇数据库 每月更新版
  14. 几种智能机器人室内定位方法对比
  15. mysql严格区分大小写吗_MySQL是否区分大小写
  16. 红米K40刷机导致基带丢失 基带未知 不读卡 没有串号 修复记录
  17. ie窗口如何最大化设置
  18. IDEA修改运行内存
  19. Windows注册表及常见子项
  20. C++stoi、stol、stoll 函数用法

热门文章

  1. laravel php配置,配置 - Configuration
  2. esxi5 的tart命令使用注意点
  3. 【游戏体验】Infiltraing the Airship(火柴人潜入飞船)
  4. 数学分析 导数与微分(第5章)
  5. gaussdb 【FAQ_002】【gs_guc配置pg_hba.conf后不生效】
  6. 我们从58 万个微信小程序中,精心挑选了这 14 个!!!
  7. C#练习题答案: 巴路士惠勒改造【难度:4级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战
  8. 悟空分词与mysql结合_悟空分词的搜索和排序源码分析之——搜索
  9. 逻辑地址、虚拟地址、线性地址、物理地址
  10. WordPress BuddyPress 存在越权漏洞RCE(CVE-2021-21389)