如何仅使用 20 和 50 的钞票从 ATM 取款?例如,如果我想提取 130 欧元,机器应该给我 1 张 50 的钞票和 4 张 20 的钞票。
要让它正常工作真的很难。有人可以帮助我吗?
我只能做这么多:
#include <stdio.h>
int main() {
int balance = 500;
int withdraw;
int bill_20, bill_50;
printf("How much you want to withdraw?");
scanf("%d", &withdraw);
if ((withdraw >= 20) && (withdraw <= balance) && (withdraw % 10 == 0)
&& (withdraw != 30)) {
if (withdraw >= 50) {
bill_50 = withdraw / 50;
withdraw = withdraw % 50;
printf("You get %d bills of 50s\n", bill_50);
}
if ((withdraw >= 20) && (withdraw < 50)) {
bill_20 = withdraw / 20;
withdraw = withdraw % 20;
printf("You get %d bills of 20s\n", bill_20);
}
} else
printf("Wrong sum");
return 0;
}
好吧,一步一步来:
- 取最大数量的 50 个。
- 如果剩下的不能在 20 秒内拿走,而我们至少拿了一个 50,放回一个 50。
- 休息时尽可能多地抽出 20 秒。
- 如果还剩下什么,那就绝望吧。
int bill_50 = withdraw / 50 - (withdraw > 50 && withdraw % 50 % 20);
int bill_20 = (withdraw - bill_50 * 50) / 20;
if (withdraw != bill_50 * 50 + bill_20 * 20)
printf("Cannot put it together.\n");
我是一名优秀的程序员,十分优秀!