我从一位经验丰富的程序员那里拿了这个问题来准备编程测试。
问题:
给定购买金额和作为付款的金额,计算作为找零返回的每种货币面额的数量和类型。总是先用最高面额的钱找零。使用的纸币面额为 100 美元、50 美元、20 美元、10 美元、5 美元和 1 美元。硬币的面额为 0.25 美元、0.10 美元、0.05 美元和 0.01 美元。
INPUT:将有 5 行输入。每行将包含 2 个有理数,分别代表购买金额和支付金额。
输出:对于每个输入行,使用使用的非零面额的数量按从高到低的大小顺序打印变化。如果没有更改到期打印 NONE。示例输出 #1 表示:1-$5、2-$1、1-$.25、2-$.10 和 2-$.01。
采样输入 采样输出
1. 12.53, 20.00 1. 121221
2. 11.33, 15.00 2. 32112
3. 35.64, 50.00 3. 14111
4. 72.67, 100.00 4. 112113 2102
5. 106.13 23102, 106.13, 5. 106.13
我决定创建两个程序:一个长的、更容易理解的程序和一个精简的版本。
这是长篇:
import java.util.*;
public class Prac13CorrectChange {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for (int A = 1; A <= 5; A++) {
System.out
.println("What is the amount of your purchase and the amount given as your payment?");
String input = in.nextLine();
String[] data = input.split(",");
double $amount = Double.parseDouble(data[0]);
double price = Double.parseDouble(data[1]);
double change = price - $amount;
int ans1 = ((int) (change / 100));
change = change - (ans1 * 100);
int ans2 = ((int) (change / 50));
change = change - (ans2 * 50);
int ans3 = ((int) (change / 20));
change = change - (ans3 * 20);
int ans4 = ((int) (change / 10));
change = change - (ans4 * 10);
int ans5 = ((int) (change / 5));
change = change - (ans5 * 5);
int ans6 = ((int) (change / 1));
change = change - (ans6 * 1);
int ans7 = ((int) (change / .25));
change = change - (ans7 * .25);
int ans8 = ((int) (change / .10));
change = change - (ans8 * .10);
int ans9 = ((int) (change / .05));
change = change - (ans9 * .05);
int ans10 = ((int) (change / .01));
change = change - (ans10 * .01);
String answer = "";
if (ans1 != 0) {
answer = answer + Integer.toString(ans1);
}
if (ans2 != 0) {
answer = answer + Integer.toString(ans2);
}
if (ans3 != 0) {
answer = answer + Integer.toString(ans3);
}
if (ans4 != 0) {
answer = answer + Integer.toString(ans4);
}
if (ans5 != 0) {
answer = answer + Integer.toString(ans5);
}
if (ans6 != 0) {
answer = answer + Integer.toString(ans6);
}
if (ans7 != 0) {
answer = answer + Integer.toString(ans7);
}
if (ans8 != 0) {
answer = answer + Integer.toString(ans8);
}
if (ans9 != 0) {
answer = answer + Integer.toString(ans9);
}
if (ans10 != 0) {
answer = answer + Integer.toString(ans10);
}
System.out.println(answer);
}
}
}
这是精简版。
import java.util.Scanner;
public class Prac15CorrectChangeShort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
for (int A = 1; A <= 5; A++) {
System.out
.println("What is the amount of your purchase and the amount given as your payment?");
String input = in.nextLine();
String[] data = input.split(",");
double $amount = Double.parseDouble(data[0]);
double price = Double.parseDouble(data[1]);
double change = price - $amount;
int ans[] = new int[10];
double money[] = { 100, 50, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01 };
for (int looptimes = 0; looptimes < 10; looptimes++) {
ans[looptimes] = ((int) (change / money[looptimes]));
change = change - (ans[looptimes] * money[looptimes]);
}
for (int looptimes = 0; looptimes < 10; looptimes++) {
if (ans[looptimes] != 0) {
System.out.print(ans[looptimes]);
}
}
System.out.print('\n');
}
}
}
我遇到的主要问题是,无论我进行什么调试,它都会给我错误的输出 #2-5。最后一位数字少了1。给我这个问题的程序员和我都找不到发生这种情况的任何原因。
请帮忙。