Допоможіть будь ласка.
В програмі тоні: в банку залишилися тільки 6 купюр : 1 грн, 1 грн, 2 грн, 2 грн, 5 грн, 10 грн. Скласти алгоритм програми, за допомогою якого можна видати всі можливі варіанти будь-якої суми(введеної з клавіатури), меншої за 21 грн, що складається з даних купюр.
Ответы на вопрос
Ответ:
#include <iostream>
#include <vector>
using namespace std;
void findCombinations(vector<int>& notes, int target, vector<int>& current, int index) {
if (target == 0) {
for (int note : current) {
cout << note << " ";
}
cout << endl;
return;
}
if (target < 0 || index == notes.size()) {
return;
}
current.push_back(notes[index]);
findCombinations(notes, target - notes[index], current, index);
current.pop_back();
findCombinations(notes, target, current, index + 1);
}
int main() {
vector<int> notes = {1, 1, 2, 2, 5, 10};
int target;
cout << "Введіть суму (меншу за 21 грн): ";
cin >> target;
if (target < 1 || target >= 21) {
cout << "Сума має бути від 1 до 20 грн." << endl;
} else {
vector<int> current;
findCombinations(notes, target, current, 0);
}
return 0;
}
Объяснение:
С++