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. vue 环境搭建笔记

    环境 开发工具:VS Code vue版本: 2.x 准备 使用 npm 包管理器进行安装,也可以使用 yarn 包管理器. 可以使用淘宝的 npm 镜像,国内速度更快. 使用方式: $ npm in ...

  2. scrapy抓取拉勾网职位信息(五)——代码优化

    上一篇我们已经让代码跑起来,各个字段也能在控制台输出,但是以item类字典的形式写的代码过于冗长,且有些字段出现的结果不统一,比如发布日期. 而且后续要把数据存到数据库,目前的字段基本都是string ...

  3. 解释Crypto模块怎么就这么"皮"?No module named "Crypto"

    https://www.cnblogs.com/fawaikuangtu123/p/9761943.html python版本:python3.6,系统:win7 1.pip install cryp ...

  4. 【BZOJ 3812】 3812: 主旋律 (容斥原理**)

    3812: 主旋律 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 235  Solved: 196 Description 响应主旋律的号召,大家决定 ...

  5. 【BZOJ 2119】 2119: 股市的预测 (后缀数组+分块+RMQ)

    2119: 股市的预测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 404  Solved: 188 Description 墨墨的妈妈热爱炒股,她 ...

  6. android fragment activity 区别

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha fragment  负责一个模块 的展示. 由 活动 管理. 碎片 可以 解决 太多活动 ...

  7. Angular Material Starter App

      介绍 Material Design反映了Google基于Android 5.0 Lollipop操作系统的原生应用UI开发理念,而AngularJS还发起了一个Angular Material ...

  8. 启动Tensorboard时发生错误:class BeholderHook(tf.estimator.SessionRunHook): AttributeError: module 'tensorflow.python.estimator.estimator_lib' has no attribute 'SessionRunHook'

    报错:class BeholderHook(tf.estimator.SessionRunHook):AttributeError: module 'tensorflow.python.estimat ...

  9. Codeforces Beta Round #10 B. Cinema Cashier 暴力

    B. Cinema Cashier 题目连接: http://www.codeforces.com/contest/10/problem/B Description All cinema halls ...

  10. 用C++/CLI搭建C++和C#之间的桥梁(一)—— 简介

    C#和C++是非常相似的两种语言,然而我们却常常将其用于两种不同的地方,C#得益于其简洁的语法和丰富的类库,常用来构建业务系统.C++则具有底层API的访问能力和拔尖的执行效率,往往用于访问底层模块和 ...