简体   繁体   English

获取任何价值的纸币和硬币数量

[英]Get the quantity of bills and coins for any value

I want to make a little system that returns me the optimized quantity of bills and coins for any value. 我想制作一个小系统,可以将任何价值的最优量的纸币和硬币退还给我。

Here's my code for while: 这是我的一段时间代码:

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");
    }
}

Well, it's almost correct, the bills are working perfect, my problem is with the coins. 好吧,这几乎是正确的,账单运作完美,我的问题是硬币。

Here are some inputs: 以下是一些输入:

  • 576.73 // Print correctly 576.73 //正确打印
  • 8.45 // Print incorrectly 8.45 //打印不正确
  • 9.45 // Print incorrectly, look below: 9.45 //打印不正确,请看下面:

Actual output: 实际输出:

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

Expected output: 预期产量:

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: I won't post all the expected outputs because it will let the question bigger than now, BUT if you need, I can post. PS:我不会发布所有预期的输出,因为它将使问题比现在大,但如果需要,我可以发布。 Thanks in advance. 提前致谢。

只需乘以100,然后以美分进行计算。

The following version of code will work as per your requirements. 以下版本的代码将根据您的要求运行。 Avoid using double in modulus operation. 避免使用双倍模数运算。

CODE: 码:

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");
    }

OUTPUT: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM