2016-04-15 5 views
0

任意の金額のために最適化された紙幣と硬貨を返すシステムを作りたいと思います。ここで任意の金額の紙幣と硬貨の数を取得

はしばらくの間、私のコードです:まあ、それはほとんど正しいです

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

、法案は完璧な作業している、私の問題はコインです。ここで

は、いくつかの入力です:

  • 576.73 //印刷正しく
  • 8.45 //印刷誤っ
  • 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 
4 coin(s) of 0.10 
0 coin(s) of 0.05 
4 coin(s) of 0.01 

Expecテッド出力:

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:それは今より質問が大きくできるようになる、しかし、あなたが必要な場合は、私が投稿することができますので、私はすべての期待される出力を掲載しません。前もって感謝します。

+2

彼らは正確に浮動小数点数として表現することができない等の問題が0.1、0.05、および0.01であると思います。整数演算を正確かつ高速に行うためには、この種の問題に整数を使用することを検討してください。 –

+0

あなたはmath.floor(rest100)を使っていますか?あなたはなぜ50セント硬貨を持っていますか? – kpie

+1

@kpieは本当に何のためにも間違いですが、(残念ながら)それは問題ではありません。私の国には50セント硬貨があります。 – developer033

答えて

0

次のバージョンのコードは、ご使用の要件に応じて動作します。モジュラス演算ではdoubleを使用しないでください。

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 
+0

それは機能しません。 9.45でテストして、予想外の出力を返します。 – developer033

+0

@ developer033コードで問題を修正しました。もう一度お試しください。 – alphablue

+0

ありがとうございました。できます!! – developer033

1

100を掛けてセントで計算します。

+1

答えをアップアップしました(他人が理由を見なかったとしても):丸め誤差のために、浮動小数点数のモジュロ演算を避けます。 100を掛け、整数を使用します。 –

+0

Thx、私はそれが間違いなく最も人間の解決策であることに同意します:) – kpie

関連する問題