如何仅使用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;
}
答案 0 :(得分:4)
好吧,分步进行:
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");
答案 1 :(得分:1)
您可以尝试每次从 withdraw
中减去该值。我这样修改了您的代码。希望这会有所帮助。
#include
int main(){
整型余额= 500;
撤回;
二十多岁= 0;
五十年代= 0;
printf(“您要提取多少?\ n”);
scanf(“%d”,&withdraw);
如果(提取>余额){
printf(“您没有这种钱。\ n”);
返回0;
}
否则,如果(提现<= 30 ||提现%10!= 0){
printf(“您只能提取20s和50s。\ n”);
返回0;
}
同时(提款> 50){
二十多岁++;
提现-= 20;
}
如果(提现== 50)五十多岁++;
否则二十多岁== 2;
printf(“您有%d 20s和%d 50s \ n”,二十,五十年代);
}