有重复数字的组合问题_带数字重复的组合和问题

有重复数字的组合问题

Description:

描述:

This is a standard interview problem to make some combination of the numbers whose sum equals to a given number using backtracking.

这是一个标准的面试问题,它使用回溯功能将总和等于给定数字的数字进行某种组合。

Problem statement:

问题陈述:

Given a set of positive numbers and a number, your task is to find out the combinations of the numbers from the set whose summation equals to the given number. You can use the numbers repeatedly to make the combination.

给定一组正数和一个数字,您的任务是从集合中找出总和等于给定数字的数字组合。 您可以重复使用数字进行组合。

    Input:
Test case T
T no. of N values and corresponding N positive numbers and the Number.
E.g.
3
3
2 3 5
12
3
2 7 5
14
4
1 2 3 4
5
Constrains:
1 <= T <= 500
1 <= N <= 20
1 <= A[i] <= 9
1 <= Number<= 50
Output:
Print all the combination which summation equals to the given number.

Example

    Input:
N=3
Set[ ]=2 3 5
Number=12
Output:
2 2 2 2 2 2
2 2 2 3 3
2 2 3 5
2 5 5
3 3 3 3

Explanation with example:

举例说明:

Let there is a set S of positive numbers N and a positive number.

设一组正数N和一个正数S。

For pre-requisite, we are recommending you to go to our article combinational sum problem.

对于先决条件,我们建议您转到我们的文章组合总和问题 。

Making some combinations repeatedly using a number in such a way that the summation of that combination results that given number is a problem of combination and we will solve this problem using backtracking approach.

重复使用数字进行一些组合,使得该组合的总和导致给定数字是组合的问题,我们将使用回溯方法解决该问题。

    Let, 
f(i) = function to insert the isth number into the combinational subset

In this case we will consider two cases to solve the problem,

在这种情况下,我们将考虑两种情况来解决该问题,

  1. We will consider ith element into the part of our combination subset until the sum greater the given number (f(i)).

    我们将考虑 i 元素进入组合子集的一部分,直到总和大于给定数( f(i) )。

  2. We will not consider ith element into the part of our combination subset (not f(i)).

    我们不会在组合子集的一部分( 不是f(i) )中考虑第ith个元素。

And every time we will check the current sum with the number. Each of the time we will count the number of occurrence and the also the combinations.

并且每次我们将用数字检查当前总和。 每次,我们将计算发生的次数以及组合。

    Let,
f(i) = function to insert the ith number into the combinational subset

For the input:

对于输入:

    S[] = {2, 3, 5}
Number = 12

Combinational sum problem with repetition of digits

Here from every (not f(2)) there will be two edges (f(3)) and (not f(3)) again from (not f(3)) there will be ( f(5)) and (not f(5)). Here in this case we will discard that edges which have a current sum greater than the given number and make a count to those numbers which are equal to the given number.

在每个(非f(2))中将有两个边(f(3))(非f(3))(非f(3))再(f(5))(非f(5)) 。 在这种情况下,我们将丢弃当前总和大于给定数字的那些边,并对等于给定数字的那些数字进行计数。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
void combination(int* arr, int n, int num, int pos, int curr_sum, vector<vector<int> >& v, vector<int> vi, set<vector<int> >& s)
{
if (num < curr_sum)
return;
if (num == curr_sum && s.find(vi) == s.end()) {
s.insert(vi);
v.push_back(vi);
return;
}
//go for the next elements for combination
for (int i = pos; i < n; i++) {
if (curr_sum + arr[i] <= num) {
vi.push_back(arr[i]);
combination(arr, n, num, i, curr_sum + arr[i], v, vi, s);
vi.pop_back();
}
}
}
//print the vector
void print(vector<vector<int> > v)
{
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << " ";
}
cout << endl;
}
}
void combinational_sum(int* arr, int n, int num)
{
vector<vector<int> > v;
vector<int> vi;
int pos = 0;
int curr_sum = 0;
set<vector<int> > s;
combination(arr, n, num, pos, curr_sum, v, vi, s);
print(v);
}
int main()
{
int t;
cout << "Test Case : ";
cin >> t;
while (t--) {
int n, num;
cout << "Enter the value of N : ";
cin >> n;
int arr[n];
cout << "Enter the values : ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
cout << "Enter the number : ";
cin >> num;
combinational_sum(arr, n, num);
}
return 0;
}

Output

输出量

Test Case : 3
Enter the value of N : 3
Enter the values : 2 3 5
Enter the number : 12
2 2 2 2 2 2
2 2 2 3 3
2 2 3 5
2 5 5
3 3 3 3
Enter the value of N : 3
Enter the values : 2 7 5
Enter the number : 14
2 2 2 2 2 2 2
2 2 5 5
2 5 7
7 7
Enter the value of N : 4
Enter the values : 1 2 3 4
Enter the number : 5
1 1 1 1 1
1 1 1 2
1 1 3
1 2 2
1 4
2 3

翻译自: https://www.includehelp.com/icp/combinational-sum-problem-with-repetition-of-digits.aspx

有重复数字的组合问题

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545422.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第四章语法分析和语法分析程序

第四章语法分析和语法分析程序4.1_自顶向下的语法分析4.1.1_自顶向下分析过程的基本特点①消除文法直接左递归②回溯的消除及LL(1)文法4.1.2_递归下降法4.1.3_预测分析法&#xff08;也叫LL1法&#xff0c;注意分析过程中非终结符号逆序入栈&#xff09;4.2_自底向上的语法分析…

实战:RediSearch 高性能的全文搜索引擎

RediSearch 是一个高性能的全文搜索引擎,它可以作为一个 Redis Module(扩展模块)运行在 Redis 服务器上。 RediSearch 主要特性如下: 基于文档的多个字段全文索引高性能增量索引文档排序(由用户在索引时手动提供)在子查询之间使用 AND 或 NOT 操作符的复杂布尔查询可选的…

智能优化算法应用:基于法医调查算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于法医调查算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于法医调查算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.法医调查算法4.实验参数设定5.算法结果6.…

转:Oracle物理文件

转载&#xff1a;http://www.worlduc.com/blog2012.aspx?bid19969111 -------------------初始化参数文件--------------------------------------------- 在9i之前&#xff0c;参数文件只有一种&#xff0c;它是文本格式的&#xff0c;称为pfile&#xff0c;在9i及以后的版本…

c语言两个浮点数相加_C语言中两个浮点数或双精度数的模数

c语言两个浮点数相加As we know that modules also known as the remainder of the two numbers can be found using the modulus (%) operator which is an arithmetic operator in C/C. The modules operator works with integer values i.e. modulus operator is used to fi…

java线程的5个使用技巧

Java线程的5个使用技巧 Published: 21 Jan 2015 Category: java Java线程有哪些不太为人所知的技巧与用法&#xff1f; 萝卜白菜各有所爱。像我就喜欢Java。学无止境&#xff0c;这也是我喜欢它的一个原因。日常工作中你所用到的工具&#xff0c;通常都有些你从来没有了解过的东…

算法复习第五章贪心法

算法复习第五章贪心法概述TSP最近邻点策略最短连接策略图着色问题最小生成树&#xff08;Prim算法、Kruskal&#xff09;0-1bag问题活动安排问题多机调度概述 TSP 最近邻点策略 最短连接策略 图着色问题 最小生成树&#xff08;Prim算法、Kruskal&#xff09; 0-1bag问题 活动…

实战:定时任务案例

我在开发的时候曾经遇到了这样一个问题,产品要求给每个在线预约看病的患者,距离预约时间的前一天发送一条提醒推送,以防止患者错过看病的时间。这个时候就要求我们给每个人设置一个定时任务,用前面文章说的延迟队列也可以实现,但延迟队列的实现方式需要开启一个无限循环任…

c语言putchar函数_C语言中的putchar()函数与示例

c语言putchar函数C语言中的putchar()函数 (putchar() function in C) The putchar() function is defined in the <stdio.h> header file. putchar()函数在<stdio.h>头文件中定义。 Prototype: 原型&#xff1a; int putchar(const char *string);Parameters: co…

算法复习第六章第七章

算法复习第六章第七章第六章回溯法TSP问题0-1bag问题图着色问题八皇后问题第七章分支限界法0-1bag问题TSP问题第六章回溯法 TSP问题 0-1bag问题 图着色问题 八皇后问题 第七章分支限界法 0-1bag问题 TSP问题

扫描识别系统

扫描识别系统&#xff0c;是指能够利用扫描仪进行扫描的相关文件&#xff0c;比方普通文档&#xff0c;政府公文&#xff0c;二代身份证&#xff0c;条码……等等。通过扫描仪扫描后不单单生成常见的JPG&#xff0c;PDF等格式的图像。而是利用先进的OCR技术&#xff0c;进行相关…

转:ORA-01126: 数据库必须已装载到此实例并且不在任何实例中打开

转载&#xff1a;http://www.worlduc.com/blog2012.aspx?bid19973952 alter database archivelog 2 ; alter database archivelog * 第 1 行出现错误: ORA-01126: 数据库必须已装载到此实例并且不在任何实例中打开 ------最佳解决方案-------------------- 修改归档模式的…

Java SimpleTimeZone setStartRule()方法与示例

SimpleTimeZone类setStartRule()方法 (SimpleTimeZone Class setStartRule() method) Syntax: 句法&#xff1a; public void setStartRule(int st_mm, int st_dd, int st_time);public void setStartRule(int st_mm, int st_dd, int st_dow, int st_time);public void setSta…

实战:Redis 性能优化方案

Redis 是基于单线程模型实现的,也就是 Redis 是使用一个线程来处理所有的客户端请求的,尽管 Redis 使用了非阻塞式 IO,并且对各种命令都做了优化(大部分命令操作时间复杂度都是 O(1)),但由于 Redis 是单线程执行的特点,因此它对性能的要求更加苛刻,本文我们将通过一些优…

Python正则表达式指南上半部

本文介绍了Python对于正则表达式的支持&#xff0c;包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例。本文的内容不包括如何编写高效的正则表达式、如何优化正则表达式&#xff0c;这些主题请查看其他教程。注意&#xff1a;本文基于Python2.4完成&#xff…

算法复习第三章分治法

算法复习第三章分治法循环日程表最近点对快速排序&#xff1a; 循环日程表 最近点对

Oracle 的 char number varchar2 效率测试

自己在建表的时候&#xff0c;用到了编号的这个字段&#xff0c; 主要问题是要用java连接数据库&#xff0c;所以要有pojo类&#xff0c; 就像 编号 这种字段&#xff0c;int 不够&#xff0c;long 转起来麻烦&#xff0c;还容易出错。 突然想看看char可不可以&#xff0c;所…

实战:Redis 慢查询

Redis 慢查询作用和 MySQL 慢查询作用类似,都是为我们查询出不合理的执行命令,然后让开发人员和运维人员一起来规避这些耗时的命令,从而让服务器更加高效和健康的运行。对于单线程的 Redis 来说,不合理的使用更是致命的,因此掌握 Redis 慢查询技能对我们来说非常的关键。 …

Java SecurityManager checkAccess()方法与示例

Syntax: 句法&#xff1a; public void checkAccess (Thread th);public void checkAccess (ThreadGroup tg);SecurityManager类的checkAccess()方法 (SecurityManager Class checkAccess() method) checkAccess(Thread th) method is called for the current security manage…

算法复习第四章动态规划

算法复习第四章动态规划动态规划TSP问题0-1bag动态规划 TSP问题 0-1bag 最长公共子序列不考&#xff1a;