[英]For a given integer a, find all unique combinations of positive integers that sum up to a
[英]How to find all possible combinations of adding two variables, each attached to a multiplier, summing up to a given number (cin)?
在我的情況下,一輛卡車的容量為 30,而一輛面包車的容量為 10。我需要找到運輸給定數量的貨物所需的貨車/卡車的數量,比如 100。我需要找到所有可能的組合卡車 + 廂式貨車加起來為 100。
基本的數學計算是:(30*lorrycount) + (10*vancount) = n,其中 n 是貨物數量。
輸出示例
運輸貨物:100
貨車數量:0 3 2 1
貨車數量:10 1 4 7
例如,第二個組合是 3 輛卡車,1 輛面包車。 考慮到卡車的容量 = 30,貨車容量 = 10,(30*3)+(10*1) = 100 = n。
目前,我們只有這段代碼,它可以在不考慮上面給出的公式的情況下,從字面上查找加起來為給定數字 n 的所有數字組合。
#include <iostream>
#include <vector>
using namespace std;
void findCombinationsUtil(int arr[], int index,
int num, int reducedNum)
{
int lorry_capacity = 30;
int van_capacity = 10;
// Base condition
if (reducedNum < 0)
return;
// If combination is found, print it
if (reducedNum == 0)
{
for (int i = 0; i < index; i++)
cout << arr[i] << " ";
cout << endl;
return;
}
// Find the previous number stored in arr[]
// It helps in maintaining increasing order
int prev = (index == 0) ? 1 : arr[index - 1];
// note loop starts from previous number
// i.e. at array location index - 1
for (int k = prev; k <= num; k++)
{
// next element of array is k
arr[index] = k;
// call recursively with reduced number
findCombinationsUtil(arr, index + 1, num,
reducedNum - k);
}
}
void findCombinations(int n)
{
// array to store the combinations
// It can contain max n elements
std::vector<int> arr(n); // allocate n elements
//find all combinations
findCombinationsUtil(&*arr.begin(), 0, n, n);
}
int main()
{
int n;
cout << "Enter the amount of cargo you want to transport: ";
cin >> n;
cout << endl;
//const int n = 10;
findCombinations(n);
return 0;
}
如果您有任何解決方案,請告訴我,謝謝。
我們將創建一個遞歸函數,從左到右遍歷全局capacities
數組,並嘗試將貨物裝載到各種車輛類型中。 我們跟蹤我們仍然需要加載多少並將其傳遞給任何遞歸調用。 如果到達數組的末尾,則僅當剩余貨物為零時才生成解決方案。
std::vector<int> capacities = { 30, 10 };
using Solution = std::vector<int>;
using Solutions = std::vector<Solution>;
void tryLoad(int remaining_cargo, int vehicle_index, Solution so_far, std::back_insert_iterator<Solutions>& solutions) {
if (vehicle_index == capacities.size()) {
if (remaining_cargo == 0) // we have a solution
*solutions++ = so_far;
return;
}
int capacity = capacities[vehicle_index];
for (int vehicles = 0; vehicles <= remaining_cargo / capacity; vehicles++) {
Solution new_solution = so_far;
new_solution.push_back(vehicles);
tryLoad(remaining_cargo - vehicles * capacity, vehicle_index + 1, new_solution, solutions);
}
}
如下調用它應該在all_solutions
產生所需的輸出:
Solutions all_solutions;
auto inserter = std::back_inserter(all_solutions)
tryLoad(100, 0, Solution{}, inserter);
尋找所有可能組合的迭代方法
#include <iostream>
#include <vector>
int main()
{
int cw = 100;
int lw = 30, vw = 10;
int maxl = cw/lw; // maximum no. of lorries that can be there
std::vector<std::pair<int,int>> solutions;
// for the inclusive range of 0 to maxl, find the corresponding no. of vans for each variant of no of lorries
for(int l = 0; l<= maxl; ++l){
bool is_integer = (cw - l*lw)%vw == 0; // only if this is true, then there is an integer which satisfies for given l
if(is_integer){
int v = (cw-l*lw)/vw; // no of vans
solutions.push_back(std::make_pair(l,v));
}
}
for( auto& solution : solutions){
std::cout<<solution.first<<" lorries and "<< solution.second<<" vans" <<std::endl;
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.