c - 仅使用 50 和 20 的钞票 C 的 ATM 取款
问题描述
如何仅使用 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;
}
解决方案
好吧,一步一步来:
- 取最大数量的 50s 适合。
- 如果剩下的在 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");
推荐阅读
- tkinter - UI 和硬件按钮
- ios - 如何添加包含新数据的临时单元格?
- php - 变量不传递给类(PHP)
- entity-framework - 从附加实体内部获取数据库上下文
- linux - 我如何能够从终端将共享库作为可执行文件运行?
- c - 使用 OpenSSL Libcrypto 循环 ASN.1 序列
- ios - 使用产品 -> 存档时找不到“FirebaseCore/FirebaseCore.h”文件
- powershell - 尝试使用小于运算符评估注册表项显示版本失败
- ajax - Laravel - 带有 Ajax 的 daterangepicker 返回 500 内部服务器错误的状态
- python - Tkinter 事件处理程序无法查看全局变量