(回溯法)数组中和为S的N个数
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个数的更多相关文章
- 【算法习题】正整数数组中和为sum的任意个数的组合数
1.递归实现(参考:https://blog.csdn.net/hit_lk/article/details/53967627) public class Test { @org.junit.Test ...
- 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数 例如给定nums = [2,7,11,15],target = 9
python解决方案 nums = [1,2,3,4,5,6] #假如这是给定的数组 target = 9 #假如这是给定的目标值 num_list = [] #用来装结果的容器 def run(nu ...
- 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 ...
- Two sum(给定一个无重复数组和目标值,查找数组中和为目标值的两个数,并输出其下标)
示例: nums = [1,2,5,7] target = [6] return [0,2] Python解决方案1: def twoSum(nums, target): ""&q ...
- 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 ...
- 使用回溯法求所有从n个元素中取m个元素的组合
不多说了,直接上代码,代码中有注释,应该不难看懂. #include <stdlib.h> #include <stdio.h> typedef char ELE_TYPE; ...
- 从Leetcode的Combination Sum系列谈起回溯法
在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...
- 算法——八皇后问题(eight queen puzzle)之回溯法求解
八皇后谜题是经典的一个问题,其解法一共有种! 其定义: 首先定义一个8*8的棋盘 我们有八个皇后在手里,目的是把八个都放在棋盘中 位于皇后的水平和垂直方向的棋格不能有其他皇后 位于皇后的斜对角线上的棋 ...
- P1120 小木棍 [数据加强版] 回溯法 终极剪枝
题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过5050. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段小木棍的长度 ...
随机推荐
- Tornado(一)
Tornado 特点 Tornado是一个用Python写的相对简单的.不设障碍的Web服务器架构,用以处理上万的同时的连接口,让实时的Web服务通畅起来.虽然跟现在的一些用Python写的Web架构 ...
- NumPy简明教程(二、数组1)
NumPy数组 NumPy数组是一个多维数组对象,称为ndarray.其由两部分组成: 实际的数据 描述这些数据的元数据 大部分操作仅针对于元数据,而不改变底层实际的数据. 关于NumPy数组有几点必 ...
- WordPress插件会员简化1.58 -任意文件下载漏洞(附poc)
今天我们将讨论在WordPress插件WordPress插件与重点会员简化v1.58作为这个剧本的创作时间不打补丁的扶贫开发实践.脆弱脚本如下: CVE-ID:cve-2017-1002008 当然, ...
- Codeforces Round #301 (Div. 2) B. School Marks 构造/贪心
B. School Marks Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/probl ...
- python函数 divmod
divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: a,b可以为数字(包括复数) from 2. Add ...
- 解决新版本webpack vue-cli生成文件没有dev.server.js问题
新版本webpack生成的dev.server.js 在webpack.dev.conf.js中 webpack.dev.conf.js const axios = require('axios') ...
- 破解ZendStudio 10.1
破解文件的网盘地址: http://pan.baidu.com/share/link?shareid=3562282358&uk=1543766223
- Transistor latch improves on/off circuitry
Figure 1 shows an example of on/off circuitry commonly used in battery-operated devices. The p-chann ...
- Spartan-6 FPGA Configuration
These configuration pins serve as the interface for a number of different configuration modes: • JTA ...
- 对printf函数的理解1
看如下代码: #include <stdio.h> int main(int argc, const char *argv[]) { printf("%s\n",&qu ...