题目连接: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的更多相关文章

  1. C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...

  2. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  3. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  4. 【leetcode】 Letter Combinations of a Phone Number(middle)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  6. 【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 ...

  7. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  8. Leetcode 17.——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

随机推荐

  1. Python 函数基础、有序集合、文件操作(三)

    一.set 特点: set是一个无序且不重复的元素集合访问速度快:天生解决元素重复问题 方法: 初始化 >>> s1 = set()>>> print(type(s ...

  2. 设计模式(四)原型模式Prototype(创建型)

      设计模式(四)原型模式Prototype(创建型) 1.   概述 我们都知道,创建型模式一般是用来创建一个新的对象,然后我们使用这个对象完成一些对象的操作,我们通过原型模式可以快速的创建一个对象 ...

  3. 评侯捷的<深入浅出MFC>和李久进的<MFC深入浅出>

    侯捷的<深入浅出mfc>相信大家都已经很熟悉了,论坛上也有很多介绍,这里我就不多说了. 而李久进的<mfc深入浅出>,听说的人可能就少得多.原因听说是这本书当时没有怎么宣传,而 ...

  4. Http方式获取网络数据

    通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据.(sax解析主要是解析xml)具体代码如下: package com.wy ...

  5. IOS SWIFT 简单操作文件

    //Home目录 let homeDirectory = NSHomeDirectory() //Documents目录 苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备 ...

  6. 虚拟化:搭建本地虚拟化环境和安装ubuntu操作系统

    本文介绍如何在本地(windows操作系统)安装虚拟机,并在虚拟机下安装ubuntu操作系统. 一.机器升级 因为是在我的笔记本电脑上操作.首先升级了我的笔记本,买了内存条,将我机器的内存增加到8G, ...

  7. 宽屏手机显示9.png的图片拉伸不均衡

    制作的一个.9的背景图片,在一般的480宽的手机上显示没有问题,正常拉伸,用三星的一个宽屏手机测试时,没有完全拉伸,一边拉伸多一点,一边拉伸少一点 决绝办法:就是在制作.9的时候,我在横向拉伸的地方, ...

  8. c语言,volatile

          一.意义: 该关键字的意义就是表示定义的变量值随时都会改变,必须从变量的地址处读取值,所以只有这个变量在使用过程中可能被改变(比如中断程序),就需要用这个关键字说明. )volatile, ...

  9. Linux权限操作 [转]

    Linux权限操作 本文内容来自<鸟哥linux私房菜>读后个人做的笔记,该书实为学习linux的很好入门教材 一.文件属性 ls ls -al列出所有的档案属性 ls是List的意思 档 ...

  10. 查询mysql哪些表正在被锁状态

    1.查进程,主要是查找被锁表的那个进程的ID SHOW PROCESSLIST; 2.kill掉锁表的进程ID KILL   10866;//后面的数字即时进程的ID