首页 > 解决方案 > 为用户在 C 中输入的金额提供最低金额的账单

问题描述

我创建了这个程序来获取用户输入的金额(只能是整数)。它应该打印收到每张账单的数量,但由于某种原因,当我运行代码时,它要求用户输入并且在我输入金额后不打印任何其他内容。我会很感激一些帮助!

#include <stdio.h>

int main() {
  double total;
  printf("Please enter the amount of money you would like to withdraw: ");
  scanf("%lf", &total);

  double hundreds;
  if (total / 100 >= 1) {
    hundreds = total / 100;
  } else {
    hundreds = 0;
    printf("You have received %lf hundred(s)", hundreds);
    total -= 100 * hundreds;
  }

  double fifties;
  if (total / 50 >= 1) {
    fifties = total / 50;
  } else {
    fifties = 0;
    printf("You have received %lf fifty(s)", fifties);
    total -= 50 * fifties;
  }

  double twenties;
  if (total / 20 >= 1) {
    twenties = total / 20;
  } else {
    twenties = 0;
    printf("You have received %lf twenty(s)", twenties);
    total -= 20 * twenties;
  }

  double tens;
  if (total / 10 >= 1) {
    tens = total / 10;
  } else {
    tens = 0;
    printf("You have received %lf ten(s)", tens);
    total -= 10 * tens;
  }

  double fives;
  if (total / 5 >= 1) {
    fives = total / 5;
  } else {
    fives = 0;
    printf("You have received %lf five(s)", fives);
    total -= 5 * fives;
  }

  double ones;
  if (total >= 1) {
    ones = total;
  } else {
    ones = 0;
    printf("You have received %lf one(s)", ones);
    total -= total;
  }
}

标签: c

解决方案


程序有误。仅在小于 时打印票据数量1

#include <stdio.h>

int main() {
    double total;
    printf("Please enter the amount of money you would like to withdraw: ");
    scanf("%lf", &total);

    double hundreds;
    if (total/100 >= 1) {
        hundreds = total / 100;
    } else {
        hundreds = 0;
        printf("You have received %lf hundred(s)", hundreds);
        total -= 100 * hundreds;
    }
    double fifties;
    if (total / 50 >= 1) {
        fifties = total / 50;
    } else {
        fifties = 0;
        printf("You have received %lf fifty(s)", fifties);
        total -= 50 * fifties;
    }
    double twenties;
    if (total / 20 >= 1) {
        twenties = total / 20;
    } else {
        twenties = 0;
        printf("You have received %lf twenty(s)", twenties);
        total -= 20 * twenties;
    }
    double tens;
    if (total / 10 >= 1) {
        tens = total / 10;
    } else {
        tens = 0;
        printf("You have received %lf ten(s)", tens);
        total -= 10 * tens;
    }
    double fives;
    if (total / 5 >= 1) {
        fives = total / 5;
    } else {
        fives = 0;
        printf("You have received %lf five(s)", fives);
        total -= 5 * fives;
    }
    double ones;
    if (total >= 1) {
        ones = total;
    } else {
        ones = 0;
        printf("You have received %lf one(s)", ones);
        total -= total;
    }
}

double此外,在除以钞票面值时不应使用算术。整数运算将正确计算票据的数量。

这是修改后的版本:

#include <stdio.h>

int main() {
    int total;
    printf("Please enter the amount of money you would like to withdraw: ");
    if (scanf("%d", &total) != 1)
        return 1;

    if (total <= 0) {
        printf("You have received no bills\n");
        return 0;
    }
    int hundreds = total / 100;
    if (hundreds) {
        printf("You have received %d hundred(s)\n", hundreds);
        total -= 100 * hundreds;
    }
    int fifties = total / 50;
    if (fifties) {
        printf("You have received %d fifty(s)\n", fifties);
        total -= 50 * fifties;
    }
    int twenties = total / 20;
    if (twenties) {
        printf("You have received %d twenty(s)\n", twenties);
        total -= 20 * twenties;
    }
    int tens = total / 10;
    if (tens) {
        printf("You have received %d ten(s)\n", tens);
        total -= 10 * tens;
    }
    int fives = total / 5;
    if (fives) {
        printf("You have received %d five(s)\n", fives);
        total -= 5 * fives;
    }
    int ones = total;
    if (ones) {
        printf("You have received %d one(s)\n", ones);
        total -= total;
    }
    return 0;
}

推荐阅读