Answer the question
In order to leave comments, you need to log in
How to solve this problem in C++?
#include <iostream>
int main() {
int sum;
std::cout << "Введите сумму, которую хотите обналичить : ";
std::cin >> sum;
if (sum > 150000) {
std::cout << "Превышено максимальное число для вывода денег.";
} else if ((sum % 5000 == 0) || ((sum - 5000) != 0)) {
int remainder;
remainder = sum - 5000;
std::cout << sum / 5000 << " купюр по 5000"; //нужно как то поместить в if
if (remainder == 0) {
std::cout << "1 купюра по 5000";
} else if (remainder % 2000 == 0) {
std::cout << " и " << remainder / 2000 << " купюр по 2000"; //еще склонения купюр надо сделать
} else if (remainder % 1000 == 0) {
std::cout << " и " << remainder / 1000 << " купюр по 1000";
} else if (remainder % 500 == 0) {
std::cout << " и " << remainder / 500 << " купюр по 500";
} else if (remainder % 200 == 0) {
std::cout << " и " << remainder / 200 << " купюр по 200";
} else if (remainder % 100 == 0) {
std::cout << " и " << remainder / 100 << " купюр по 100";
}
}/* else if ((sum % 2000 == 0) || ((sum - 2000) != 0)) {
int remainder = sum - 2000;
//std::cout << sum / 2000 << " купюр по 2000";
if (remainder == 0) {
std::cout << "1 купюра по 2000";
}
else if (remainder % 1000 == 0 && remainder != 0) {
std::cout << " и " << remainder / 1000 << " купюр по 1000";
} else if (remainder % 500 == 0 && remainder != 0) {
std::cout << " и " << remainder / 500 << " купюр по 500";
} else if (remainder % 200 == 0 && remainder != 0) {
std::cout << " и " << remainder / 200 << " купюр по 200";
} else if (remainder % 100 == 0 && remainder != 0) {
std::cout << " и " << remainder / 100 << " купюр по 100";
}
}
else if ((sum % 1000 == 0) && ((sum - 1000) != 0)) {
int remainder = sum - 1000;
std::cout << sum / 1000 << " купюр по 1000";
if (remainder % 500 == 0) {
std::cout << " и " << remainder / 500 << " купюр по 500";
} else if (remainder % 200 == 0 && remainder != 0) {
std::cout << " и " << remainder / 200 << " купюр по 200";
} else if (remainder % 100 == 0 && remainder != 0) {
std::cout << " и " << remainder / 100 << " купюр по 100";
}
}
else if ((sum % 500 == 0) && ((sum - 500) != 0)) {
int remainder = sum - 500;
std::cout << sum / 500 << " купюр по 500";
if (remainder % 200 == 0) {
std::cout << " и " << remainder / 200 << " купюр по 200";
} else if (remainder % 100 == 0 && remainder != 0) {
std::cout << " и " << remainder / 100 << " купюр по 100";
}
}
else if ((sum % 200 == 0) && ((sum - 200) != 0)) {
int remainder = sum - 200;
std::cout << sum / 200 << " купюр по 200";
if (remainder % 100 == 0 && remainder != 0) {
std::cout << " и " << remainder / 100 << " купюр по 100";
}
}
else if ((sum % 100 == 0) && ((sum - 100) != 0)) {
int remainder = sum - 100;
std::cout << sum / 100 << " купюр по 100";
} else {
std::cout << "Такую сумму вывести невозможно";
}*/
}
Answer the question
In order to leave comments, you need to log in
Having sorted out different amounts a little with your hands, you can understand that a greedy solution works here - you can take the largest bill that fits into the remaining amount.
This can also be proven. It makes no sense to take more than one 100, they could be replaced by 200. It also makes no sense to take more than two 200 - three can be exchanged for 500 + 100, which is less than bills. Similarly for all remaining banknotes.
Well, it remains for you to check for each bill that the remaining amount is not less than the bill, then give out how many bills fit and replace the amount with the remainder of the division.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question