Given a list of numbers, find the number of tuples of size N that add to S.

for example in the list (10,5,-1,3,4,-6), the tuple of size 4 (-1,3,4,-6) adds to 0.

题目:

给一数组,求数组中和为S的N个数

思路:

回溯法,数组中每个数都有两种选择,取或者不取;

当选择的数等于N时,则判断该数之和是否等于S。

代码:

#include <iostream>
#include <vector> using namespace std; void GetSum(int sum,vector<int> &result,int *num,int n,int curSum,int index,int count){
if(count==n+1)
return; if(index==4){
if(curSum==sum){
for(int i=0;i<4;i++)
cout<<result[i]<<" ";
cout<<endl;
}
return;
} int x=num[count];
result.push_back(x);
GetSum(sum,result,num,n,curSum+x,index+1,count+1);
result.pop_back();
GetSum(sum,result,num,n,curSum,index,count+1);
} int main()
{
int nums[]={10,-5,-5,0,5,4,6};
int len=sizeof(nums)/sizeof(nums[0]);
int curSum=0;
int index=0;
int count=0;
int sum=0;
vector<int> result;
GetSum(sum,result,nums,len,curSum,index,count);
return 0;
}

(回溯法)数组中和为S的N个数的更多相关文章

  1. 【算法习题】正整数数组中和为sum的任意个数的组合数

    1.递归实现(参考:https://blog.csdn.net/hit_lk/article/details/53967627) public class Test { @org.junit.Test ...

  2. 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数 例如给定nums = [2,7,11,15],target = 9

    python解决方案 nums = [1,2,3,4,5,6] #假如这是给定的数组 target = 9 #假如这是给定的目标值 num_list = [] #用来装结果的容器 def run(nu ...

  3. 18. 4Sum -- 找到数组中和为target的4个数

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  4. Two sum(给定一个无重复数组和目标值,查找数组中和为目标值的两个数,并输出其下标)

    示例: nums = [1,2,5,7] target = [6] return [0,2] Python解决方案1: def twoSum(nums, target): ""&q ...

  5. leetcode_401_Binary Watch_回溯法_java实现

    题目: A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bot ...

  6. 使用回溯法求所有从n个元素中取m个元素的组合

    不多说了,直接上代码,代码中有注释,应该不难看懂. #include <stdlib.h> #include <stdio.h> typedef char ELE_TYPE; ...

  7. 从Leetcode的Combination Sum系列谈起回溯法

    在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...

  8. 算法——八皇后问题(eight queen puzzle)之回溯法求解

    八皇后谜题是经典的一个问题,其解法一共有种! 其定义: 首先定义一个8*8的棋盘 我们有八个皇后在手里,目的是把八个都放在棋盘中 位于皇后的水平和垂直方向的棋格不能有其他皇后 位于皇后的斜对角线上的棋 ...

  9. P1120 小木棍 [数据加强版] 回溯法 终极剪枝

    题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过5050. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段小木棍的长度 ...

随机推荐

  1. Linux命令之usermod

    usermod [选项] 登录名 usermod修改用户基本信息. (1).常用选项 -d,--home HOME_DIR 用户的新主目录 -g,--gid GROUP 强制GROUP为新主组 -G, ...

  2. POJ 1741 树分治

    题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典 ...

  3. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

    Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...

  4. 「BZOJ 4502」串

    「BZOJ 4502」串 题目描述 兔子们在玩字符串的游戏.首先,它们拿出了一个字符串集合 \(S\),然后它们定义一个字符串为"好"的,当且仅当它可以被分成非空的两段,其中每一段 ...

  5. [转]runOnUiThread 、 Handler 对比(一)

      this.runOnUiThread(new Runnable() { @Override public void run() { try { Thread.sleep(1000 * 5); }  ...

  6. [转]Eclipse 的快捷键以及文档注释、多行注释的快捷键

    一.多行注释快捷键 1.选中你要加注释的区域,用ctrl+shift+C 或者ctrl+/ 会加上//注释 2.先把你要注释的东西选中,用shit+ctrl+/ 会加上/*    */注释 3.以上快 ...

  7. VK Cup 2016 - Qualification Round 2 D. Three-dimensional Turtle Super Computer 暴力

    D. Three-dimensional Turtle Super Computer 题目连接: http://www.codeforces.com/contest/638/problem/D Des ...

  8. mybatis源码分析(5)-----拦截器的实现原理(动态代理+责任链)

    写在前面 MyBatsi 的拦截器模式是基于代理的代理模式.并且myBatis 的插件开发也是以拦截器的形式集成到myBatis 当中. MyBatis 的拦截器已经插件是在org.apache.ib ...

  9. Spring发送带附件邮件

    下面是一个例子使用Spring通过Gmail SMTP服务器来发送电子邮件附件.为了包含附件的电子邮件,你必须使用 Spring的JavaMailSender及MimeMessage 来代替 Mail ...

  10. source insight 设置自动缩进

    转:http://biancheng.dnbcw.info/c/283027.html 使用source insight 的时候一直被一个问题困扰着:就是在 {  后如果敲下回车后,光标换行后,但没有 ...