2011-12-06 9 views
-1

今日は物理学で2つの小さなスクリプトを作ったが、今はバグが始まっている。カウントの意見の差

最初のスクリプトは100%正確です。これは、希望額の現金に必要な請求書とコインの数を計算するために使用されます。

最初のスクリプト:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class Change { 

static Money[] coins; 
static int[] counts; 

public static void main(String[] args) throws IOException { 
    coins = new Money[11]; 
    counts = new int[11]; 
    coins[0] = new Money(100); 
    coins[1] = new Money(50); 
    coins[2] = new Money(20); 
    coins[3] = new Money(10); 
    coins[4] = new Money(5); 
    coins[5] = new Money(2); 
    coins[6] = new Money(1); 
    coins[7] = new Money(25, true); 
    coins[8] = new Money(10, true); 
    coins[9] = new Money(5, true); 
    coins[10] = new Money(1, true); 
    System.out.println("Please type the change:\n"); 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    String values = br.readLine(); 
    String[] split = values.split("\\."); 
    System.out.println(); 
    int whole = Integer.parseInt(split[0]); 
    int small = Integer.parseInt(split[1]); 
    for (int i = 0; i < 7; i++) { 
     while (whole >= coins[i].getValue()) { 
      whole -= coins[i].getValue(); 
      counts[i]++; 
     } 
    } 
    for (int i = 7; i < 11; i++) { 
     while (small >= coins[i].getValue()) { 
      small -= coins[i].getValue(); 
      counts[i]++; 
     } 
    } 
    for (int i = 0; i < 11; i++) { 
     if (counts[i] > 0) 
      System.out 
        .println((coins[i].getValue() == 100 ? "" : " ") 
          + (coins[i].isDecimal() ? (" 0." 
            + (coins[i].getValue() < 10 ? "0" : "") + coins[i] 
             .getValue()) + ": " + counts[i] 
            : ((coins[i].getValue() <= 5 ? " " : "") + coins[i] 
              .getValue()) 
              + ".00: " 
              + counts[i])); 

    } 
} 

public static class Money { 

    int value; 
    boolean decimal; 

    Money(int value) { 
     this(value, false); 
    } 

    Money(int value, boolean decimal) { 
     this.value = value; 
     this.decimal = decimal; 
    } 

    boolean isDecimal() { 
     return decimal; 
    } 

    int getValue() { 
     return value; 
    } 
} 
} 

2番目のスクリプト:

import java.io.IOException; 

public class ChangeMax { 


static Money[] coins; 
static int[] nums = new int[2]; 
static int max = -2147483648; 

public static void main(String[] args) throws IOException{ 
    coins = new Money[11]; 
    coins[0] = new Money(100); 
    coins[1] = new Money(50); 
    coins[2] = new Money(20); 
    coins[3] = new Money(10); 
    coins[4] = new Money(5); 
    coins[5] = new Money(2); 
    coins[6] = new Money(1); 
    coins[7] = new Money(25, true); 
    coins[8] = new Money(10, true); 
    coins[9] = new Money(5, true); 
    coins[10] = new Money(1, true); 
    for(int i = 0; i < 100; i++){ 
     int temp1 = i; 
     for(int h = 1; h < 100; h++){ 
      int temp2 = h; 
      int[] counts = new int[100]; 
      for (int j = 0; j < 7; j++) { 
       while (temp1 >= coins[j].getValue()) { 
        temp1 -= coins[j].getValue(); 
        counts[j]++; 
       } 
      } 
      for (int k = 7; k < 11; k++) { 
       while (temp2 >= coins[k].getValue()) { 
        temp2 -= coins[k].getValue(); 
        counts[k]++; 
       } 
      } 
      int sum = 0; 
      for(int p : counts){ 
       sum += p; 
      } 
      if(sum > max){ 
       max = sum; 
       nums[0] = i; 
       nums[1] = h; 
      } 
     } 
    } 
    System.out.println("\nMax coins and bills required at: $"+nums[0]+"."+(nums[1] > 9 ? nums[1] : "0" + nums[1]) + ": "+max+"\n"); 
} 

public static class Money { 

    int value; 
    boolean decimal; 

    Money(int value) { 
     this(value, false); 
    } 

    Money(int value, boolean decimal) { 
     this.value = value; 
     this.decimal = decimal; 
    } 

    boolean isDecimal() { 
     return decimal; 
    } 

    int getValue() { 
     return value; 
    } 
} 

} 

2番目のスクリプトは、同じことを行いますが、$ 100の下部にすべての値を介して実行されます。

問題は、2番目のスクリプトで最大量が9で、$ 0.94で達成されているということです。あなたは$ 1.94のようなものを入力したとき

まず、スクリプト、10が問題のようです何の代わりに9

の、新最高数であることを登録しませんか?

+0

あなたが必要とするコインの最小枚数を意味しますか? 「最大」は94になります。 – user1071777

+4

物理学級でCSの宿題をする理由は何ですか? CSクラスで物理ラボをやっていますか? – emory

+0

私の学校はコンピュータサイエンスコースを提供していません。 @ user1071777必要な請求書とコインの最大額を計算したいと考えています。お返事ありがとうございますが、最初のプログラムが示しているようにいくつかのエラーがあるようです。 1ドルまたは3ドルを追加するだけで、それぞれ10ドルおよび11ドルになります。なぜこれが正しく動作しないのでしょうか? – Matt

答えて

0

あなたのお金のオブジェクトは、例えばの値かどうかを知っている)私は、私が働いてコードを提供するつもりはない、あなたの宿題を行う上で計画していないのですが、両方のスクリプトを簡単

1を改善することができるので、 10ドルは10ドル、つまり10セントです(アメリカではユーロとセントを使います)。しかし、まだあなたはセント

2にドルから切り替えて使用しているアレイのハードコーディングされたインデックスを使用)誰かが

3小数部なしで、入力として素敵な丸みを帯びた番号を使用する場合、最初のスクリプトが失敗した場合)入力をまずセントに変換し、コイン配列内のすべての値も変換すると、コードははるかにクリーンでわかりやすくなります。

の形のもの
int startAmount = ... ;//in cents 
int remainder = startAmount; 
int coinCounter = new int[coins.length]; 
for (int i = 0; i < coins.length; i++){ 
    int currentCoin = coins[i];//in cents 
    coinCointer[i] = 0; 
    while(remainder >= currentCoin){ 
    coinCointer[i] = coinCointer[i] + 1; 
    remainder = remainder - currentCoin; 
    } 
} 
//print out by looping over coinCounter, 
//and use the info contained in the Money class 
関連する問題