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. localstorage跟sessionstorage

    能查到证明有人指引,直接上代码 <script type="text/javascript"> function cunchu1(){ var arr = [ 1, 2 ...

  2. Am335x U-boot LCD简易驱动

    参考此文档说明,自行添加相关代码: https://pan.baidu.com/s/1i5gLE89 相关代码: https://pan.baidu.com/s/1qXL8Bne 在文档说明第四步1中 ...

  3. Divisible by Seven CodeForces - 376C (数论)

    You have number a, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange ...

  4. poj 1659 Frogs' Neighborhood( 青蛙的邻居)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9639   Accepted: 40 ...

  5. git实现github仓库和本地仓库同步

    配置git 安装git以后,打开git bash,首先要对git进行配置,输入 git config --global username "你的名字" git config --g ...

  6. 「APIO2018选圆圈」

    「APIO2018选圆圈」 题目描述 在平面上,有 \(n\) 个圆,记为 \(c_1, c_2, \ldots, c_n\) .我们尝试对这些圆运行这个算法: 找到这些圆中半径最大的.如果有多个半径 ...

  7. Problem G: 深入浅出学算法008-求佩尔方程的解

    Description 求关于x y的二次不定方程的解 x2-ny2=1 Input 多组输入数据,先输入组数T 然后输入正整数n(n<=100) Output 对于每组数据输出一行,求y< ...

  8. php -- 解决php连接sqlserver2005中文乱码问题(附详细解决方法)

    @_@~~ --php5.2 --phpstudy --apache --sqlserver2005 @_@~~问题描述 问题一:php连接sqlsever2005,输入中文,然后查询sqlserve ...

  9. Educational Codeforces Round 11 A. Co-prime Array 水题

    A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...

  10. Codeforces Beta Round #7 B. Memory Manager 模拟题

    B. Memory Manager 题目连接: http://www.codeforces.com/contest/7/problem/B Description There is little ti ...