[LeetCode] Print All Combinations of a Number as a Sum of Candidate Numbers
题目连接:http://leetcode.com/2010/09/print-all-combinations-of-number-as-sum.html
题目分析:
由于这里说明了输入是升序的,当然如果是乱序的输入,只要没有要求输出有序,就不需要排序,否则在计算时,先对数组进行排序处理。
假设当前的arr[i]比Sum小,则计入arr[i],并更新Sum的值。否则,跳过当前的arr[i]元素。
题目扩展和变形:
假设这里的数字是不能重复的,参见不重复求和。
参考代码:
void Solve(const int *arr, const int nLen, int *ans, const int Num, int Sum)
{
assert(arr && ans && Num >= 0 && Sum >= 0);
if(Sum == 0)
{
Print(ans, Num);
return;
}
if(nLen <= 0)
{
return;
} for(int i = nLen - 1; i >= 0; --i)
{
if(arr[i] <= Sum)
{
//如果当前数可以取,取当前的i
ans[Num] = arr[i];
//i+1表明当前取的元素,在下一次还可以取,如果每个元素只能用一次,则为i
Solve(arr, i + 1, ans, Num + 1, Sum - arr[i]);
} }
}
照旧,最后给出一些辅助函数和main函数的调用:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h> void Print(const int *arr, const int nLen)
{
for(int i = 0; i < nLen; ++i)
{
printf("%d ", arr[i]);
}
printf("\n");
} int Compare(const void* a, const void* b)
{
return (int)a - (int)b;
} int main()
{
const int MAX_N = 30;
int arr[MAX_N];
int ans[3 * MAX_N];
int Sum;
int n,i;
while(scanf("%d %d", &n, &Sum) != EOF)
{
for(i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
}
//如果输入不是升序的,可以先排序
//qsort(arr, n, sizeof(int), Compare);
Solve(arr, n, ans, 0, Sum);
}
}
[LeetCode] Print All Combinations of a Number as a Sum of Candidate Numbers的更多相关文章
- C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【leetcode】 Letter Combinations of a Phone Number(middle)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
随机推荐
- ZOJ 3331 Process the Tasks 双塔Dp
用dp[i][j]表示当前安排好了前i个任务,且机器A和机器B完成当前分配到的所有任务的时间差为j(这里j可正可负,实现的时候需要加个offset)时,完成这些任务的最早时间.然后根据j的正负,分别考 ...
- ZOJ 2853 Evolution 【简单矩阵快速幂】
这道题目第二次看的时候才彻底理解了是什么意思 把题目转化为数学模型分析后就是 有一个初始序列, 有一个进化率矩阵 求的是初始序列 与进化率矩阵进行 m 次运算后, 初始序列最后一位的答案 那么显然,可 ...
- USACO Barn Repair 【贪心算法】
这到题目的题意不太好理解= = 看来还是英语太弱了 实际上题目给了你M, S, C 分别代表最多不超过M 块木板, S代表牛棚总数,C代表接下来有C个牛所在牛棚的标号 然后求的是如何安排方案,可以使得 ...
- Codeforces 484A - Bits 二进制找1
这题可以根据l, r 在二进制下的长度进行分类. l 的长度小于 r 的时候,有两种可能,一种是r 在二进制下是 1* 这种样子,故答案取 r : 一种是取答案为 (1LL << (r ...
- C#实现时间戳转化
/// <summary> /// 时间戳转为C#格式时间 /// </summary> /// <param name=”timeStamp”></para ...
- 我也来说说C#中的异步:async/await
序 最近看了一些园友们写的有关于异步的文章,受益匪浅,写这篇文章的目的是想把自己之前看到的文章做一个总结,同时也希望通过更加通俗易懂的语言让大家了解"异步"编程. 1:什么是异步 ...
- 设计模式(十二)职责链模式(Chain of Responsibility)(对象行为型)
设计模式(十二)职责链模式(Chain of Responsibility)(对象行为型) 1.概述 你去政府部门求人办事过吗?有时候你会遇到过官员踢球推责,你的问题在我这里能解决就解决,不能解决就 ...
- 模式匹配KMP
字符串朴素模式匹配算法的2种实现: //1.朴素的模式匹配算法,用while实现 int StrStr_While(const char* pStr, const char* pSub, int* p ...
- Oracle 数据的导入和导出(SID service.msc)
一:版本号说明: (1)(Oracle11 32位系统)Oracle - OraDb11g_home1: (2)成功安装后显演示样例如以下:第一个图是管理工具.创建连接.创建表:第二个是数据库创建工 ...
- 【翻译自mos文章】当指定asm disk 为FRA时,11.2.0.3的dbua hang住
当指定asm disk 为FRA时.11.2.0.3的dbua hang住 来源于: 11.2.0.3 DBUA Hangs While Specifying ASM Disk To FRA (文档 ...