[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 ...
随机推荐
- jQuery UI Widget 原理
先看下代码的相关注释: /*! * jQuery UI Widget 1.8.1 * * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/abo ...
- String "+" 的补充说明---行粒度
String 中“+” 的操作的补充说明 在使用“+”的时候,会创建一个StringBuilder对象,然后invokevirtual append()操作 “+”操作创建StringBuilder的 ...
- QT5.6所开放的7个新模块(图表,虚拟键盘,性能分析,静态分析,测试正好,2D渲染)
The modules newly available to open source users are: Qt Charts Qt Data Visualization Qt Virtual Key ...
- 同步机制Lock初学(转)
前段时间review同事的代码,发现可以简化成如下的模型: Thread 1 Thread 2 lock.lock(); condition.await(); lock.unlock() lock.l ...
- Java网络蜘蛛/网络爬虫 Spiderman
Spiderman - 又一个Java网络蜘蛛/爬虫 Spiderman 是一个基于微内核+插件式架构的网络蜘蛛,它的目标是通过简单的方法就能将复杂的目标网页信息抓取并解析为自己所需要的业务数据. 主 ...
- matlab绘图方法汇总
Matlab画图 强大的画图功能是Matlab的特点之中的一个,Matlab提供了一系列的画图函数,用户不须要过多的考虑画图的细节,仅仅须要给出一些基本參数就能得到所需图形,这类函数称为高层画图函数. ...
- CF(435D - Special Grid)dp
题目链接:http://codeforces.com/problemset/problem/435/D 题意:求三角形个数,三个点必须的白点上,而且三条边必须是横线,竖线或对角线,三条边上不同意出现黑 ...
- oracle scn浅析
1. 系统SCN号 查询系统SCN号的方法: select dbms_flashback.get_system_change_number from dual commit后系统SCN号会增长,但是即 ...
- 一款简单的客户端安卓手机qq源码
给大家分享一款比较简单的安卓手机qq应用源码,效果非常不错,大家可以借鉴一下,希望大家会喜欢. 1.png (7.24 KB, 下载次数: 0) 02.png (68.52 KB, 下载次数: 0 ...
- stm32之CAN发送、接收详解
CAN接收报文并过滤之标识符过滤:(重点.难点) 在CAN协议里,报文的标识符不代表节点的地址,而是跟报文的内容相关的.因此,发送者以广播的形式把报文发送给所有的接收者.节点在接收报文时-根据标识符的 ...