我想制作一个小系统,为我提供任何价值的优化数量的账单和硬币。
这是我的代码:
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
double amount = Double.parseDouble(br.readLine());
if (amount > 0 && amount < 1000000.00) {
// ############################# BILLS ############################
double rest100 = amount / 100;
double rest50 = amount % 100;
double rest20 = rest50 % 50;
double rest10 = rest20 % 20;
double rest5 = rest10 % 10;
double rest2 = rest5 % 5;
// ############################ COINS ############################
double rest01 = rest2 % 2;
double rest050 = rest01 % 1;
double rest025 = rest050 % .5;
double rest010 = rest025 % 25;
double rest005 = rest010 % .1;
double rest001 = rest005 % .05;
System.out.println("BILLS:\n"
+ (int) rest100
+ " bill(s) of 100.00\n"
+ (int) rest50 / 50
+ " bill(s) of 50.00\n"
+ (int) rest20 / 20
+ " bill(s) of 20.00\n"
+ (int) rest10 / 10
+ " bill(s) of 10.00\n"
+ (int) rest5 / 5
+ " bill(s) of 5.00\n"
+ (int) rest2 / 2
+ " bill(s) of 2.00\n"
+ "COINS:\n"
+ (int) (rest01 / 1)
+ " coin(s) of 1.00\n"
+ (int) (rest050 / .5)
+ " coin(s) of 0.50\n"
+ (int) (rest025 / .25)
+ " coin(s) of 0.25\n"
+ (int) (rest010 / .1)
+ " coin(s) of 0.10\n"
+ (int) (rest005 / .05)
+ " coin(s) of 0.05\n"
+ (int) (rest001 / .01)
+ " coin(s) of 0.01");
}
}
嗯,这几乎是正确的,账单工作正常,我的问题在于硬币。
以下是一些输入:
实际输出:
BILLS:
0 bill(s) of 100.00
0 bill(s) of 50.00
0 bill(s) of 20.00
0 bill(s) of 10.00
1 bill(s) of 5.00
2 bill(s) of 2.00
COINS:
0 coin(s) of 1.00
0 coin(s) of 0.50
1 coin(s) of 0.25
4 coin(s) of 0.10
0 coin(s) of 0.05
4 coin(s) of 0.01
预期产出:
BILLS:
0 bill(s) of 100.00
0 bill(s) of 50.00
0 bill(s) of 20.00
0 bill(s) of 10.00
1 bill(s) of 5.00
2 bill(s) of 2.00
COINS:
0 coin(s) of 1.00
0 coin(s) of 0.50
1 coin(s) of 0.25
2 coin(s) of 0.10
0 coin(s) of 0.05
0 coin(s) of 0.01
PS:我不会发布所有预期的输出,因为它会让问题比现在更大,但如果你需要,我可以发帖。提前谢谢。
答案 0 :(得分:1)
只需乘以100并以美分计算。
答案 1 :(得分:0)
以下版本的代码将根据您的要求运行。避免使用双模数运算。
代码:
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
double amount = Double.parseDouble(br.readLine());
amount= Math.round(amount*100);
if (amount > 0 && amount < 1000000.00) {
// ############################# BILLS ############################
double rest100 = amount / 10000;
double rest50 = amount % 10000;
double rest20 = rest50 % 5000;
double rest10 = rest20 % 2000;
double rest5 = rest10 % 1000;
double rest2 = rest5 % 500;
// ############################ COINS ############################
double rest01 = rest2 % 200;
double rest050 = rest01 % 100;
double rest025 = rest050 % 50;
double rest010 = rest025 % 25;
double rest005 = rest010 % 10;
double rest001 = rest005 % 5;
System.out.println("BILLS:\n"
+ (int) Math.floor(rest100)
+ " bill(s) of 100.00\n"
+ (int) (rest50 / 5000)
+ " bill(s) of 50.00\n"
+ (int) (rest20 / 2000)
+ " bill(s) of 20.00\n"
+ (int) (rest10 / 1000)
+ " bill(s) of 10.00\n"
+ (int) (rest5 / 500)
+ " bill(s) of 5.00\n"
+ (int) (rest2 / 200)
+ " bill(s) of 2.00\n"
+ "COINS:\n"
+ (int) (rest01 / 100)
+ " coin(s) of 1.00\n"
+ (int) (rest050 / 50)
+ " coin(s) of 0.50\n"
+ (int) (rest025 / 25)
+ " coin(s) of 0.25\n"
+ (int) (rest010 / 10)
+ " coin(s) of 0.10\n"
+ (int) (rest005 / 5)
+ " coin(s) of 0.05\n"
+ (int) (rest001 / 1)
+ " coin(s) of 0.01");
}
输出:
9.45
BILLS:
0 bill(s) of 100.00
0 bill(s) of 50.00
0 bill(s) of 20.00
0 bill(s) of 10.00
1 bill(s) of 5.00
2 bill(s) of 2.00
COINS:
0 coin(s) of 1.00
0 coin(s) of 0.50
1 coin(s) of 0.25
2 coin(s) of 0.10
0 coin(s) of 0.05
0 coin(s) of 0.01