日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 有重复数字的组合问题_带数字重复的组合和问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有重復數字的組合問題

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 TT no. of N values and corresponding N positive numbers and the Number.E.g.332 3 51232 7 51441 2 3 45Constrains:1 <= T <= 5001 <= N <= 201 <= A[i] <= 91 <= Number<= 50Output:Print all the combination which summation equals to the given number.

Example

Input:N=3Set[ ]=2 3 5Number=12Output:2 2 2 2 2 22 2 2 3 32 2 3 52 5 53 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,

在這種情況下,我們將考慮兩種情況來解決該問題,

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

    我們將考慮 i 元素進入組合子集的一部分,直到總和大于給定數( f(i) )。

  • 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

    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 combinationfor (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

    有重復數字的組合問題

    總結

    以上是生活随笔為你收集整理的有重复数字的组合问题_带数字重复的组合和问题的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。