[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 ...
随机推荐
- C#注册表
C#注册表情缘 记得当时刚接触C#的时候,喜欢编写各种小软件,而注册表系列和网络系列被当时的我认为大牛的必备技能.直到我研究注册表前一天我都感觉他是那么的高深. 今天正好有空,于是就研究了下注册表 ...
- CentOS 添加常用 yum 源
CentOS 的官方源去掉了一些与版权有关的软件,因此想要安装这些软件或者手动下载安装,或者使用其他源. 下面我推荐常用的两个源, 这两个源基本可以满足一般服务器的使用需求. 首先, 添加源之前要确定 ...
- Android常用动画Animation的使用
Andriod中有几种常用的Animation AlphaAnimation 淡入淡出效果 RotateAnimation 旋转效果 ScaleAnimation 缩放动画 TranslaAnima ...
- C语言实现单链表的逆置
单链表的逆置是一个非常经典的问题,这里利用两个思想进行解决. 首先,我们需要看下原理图,其实两个思想都是一样的,都是使后一个的节点的 next 指针指向前一个节点,依次递推,直 ...
- 点击datalist中Button按钮出现“回发或回调参数无效......”
遇到问题: 回发或回调参数无效.在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page ...
- Oracle 11gR2的完全卸载
首先停止oracle服务,卸载oracle,其次删除oracle文件夹,最后删除oracle服务和清理注册表. 以下是详细教程 1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: ...
- Java --CountDownLatch简介
CountDownLatch 1.类介绍 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.用给定的计数 初始化 CountDownLatch.由于调用了 coun ...
- C# MVC 自学笔记—5 添加模型
==============================翻译============================== 在本节中,您将添加一些类来管理数据库中的电影.这些类将 ASP.NET M ...
- JDBC连接SQL server与ADO.NET连接Sql Server对比
JDBC连接SQL server与ADO.NET连接Sql Server对比 1.JDBC连接SQL server 1)java方面目前有很多驱动能够驱动连接SQL servernet. 主流的有 ...
- SQLServer分页查询存储过程
项目中用到的SQLServer分页查询存储过程. [存储过程] create PROCEDURE prcPageResult -- 获得某一页的数据 -- @currPage int = 1, ...