首页 > 解决方案 > 仅使用 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;
}

标签: c

解决方案


好吧,一步一步来:

  1. 取最大数量的 50s 适合。
  2. 如果剩下的在 20 秒内不能拿走,而我们至少拿了一个 50,就把一个 50 放回去。
  3. 剩下的时间尽可能多地取 20 多岁。
  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");

推荐阅读