[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 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0607strcpy
题目
- Cannot drop the database ‘XXX’ because it is being used for replication.
删除订阅数据库的时候出现下面的错误: Cannot drop the database ‘XXX’ because it is being used for replication. 数据库的状态为 ...
- [产值分析]生产部KPI考核之产值分析
接到新任务:设计统计电子和磁电公司生产部产值分析报表. 眼下状况: 1.电子公司:取最新单位价格*入库数量 2.磁电公司:取最低价格*入库数量(实际取价的时候又没有取到最低价) 假设计算出来的结果和財 ...
- parquet 合并元数据
合并元数据:两个数据集,有着一部分相同的列,将他们合并成一个数据集时merge的过程. 合并的规则:相同的列,在新的数据集中,是通用的列, 各自不同的列,也作为新的数据集的列. Spark将数据写入到 ...
- IOS中的数据存储 简单总结
1. NSKeyedArchiver(加密形式) 2. plist 3. NSUserDefaults 4. writeToFile 5. SQLite3 ==== N ...
- BZOJ 1025: [SCOI2009]游戏( 背包dp )
显然题目要求长度为n的置换中各个循环长度的lcm有多少种情况. 判断一个数m是否是满足题意的lcm. m = ∏ piai, 当∑piai ≤ n时是满足题意的. 最简单我们令循环长度分别为piai, ...
- [转]CentOS_yum的详细使用方法
yum 是什么yum = Yellow dog Updater, Modified主要功能是更方便的添加/删除/更新RPM包.它能自动解决包的倚赖性问题.它能便于管理大量系统的更新问题 yum特点可以 ...
- 高级UIKit-06(UIImagePickerController)
[day07-1-getSystemImage]:获取系统相册 UIImagePickerController图片采集控制器 picker采集者,采摘者 该方法继承自:UINavigationCont ...
- ActivityGroup相关--getLocalActivityManager() 以及intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)用法
ActivityGroup简介 1.ActivityGroup的核心就是继承了该类,能够通过getLocalActivityManager()得到一个LocalActivityManager 如,Lo ...
- Laravel创建Model
它已被用于CI框架.最近学习使用Laravel框架,要总结一些遇到的问题是一个创纪录,供以后调用.此外,我希望能够碰到同样的问题的朋友的帮助. 在Laravel数据库表是根据Laravel写好的程序去 ...