对于我的一个课程,我们必须找到一种方法,在给定任意数量的硬币的情况下,以最少的硬币进行一定量的更改。以下是我的代码:
public class Changemaker {
public static void main ( String[] args ) {
int amount = Integer.parseInt(args[args.length-1]);
int coins = args.length - 1;
if (amount < 0){
throw new IllegalArgumentException("IMPROPER AMOUNT");
} else {
for (int i = 1; i <= coins; i++) {
int coin = Integer.parseInt(args[i]);
if (coin <= 0){
throw new IllegalArgumentException("IMPROPER DENOMINATION");
} else {
for (int j = 1; j <= coins; j++) {
int validCoin = Integer.parseInt(args[j]);
if (validCoin == coin && j != i ) {
throw new IllegalArgumentException("DUPLICATE DENOMINATION");
}
}
}
try {
String firstCoin = args[1];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("INSUFFICIENT DATA");
throw new ArrayIndexOutOfBoundsException(" ");
}
}
}
Tuple [][] table = new Tuple [coins][amount + 1];
for(int x = 0; x < coins; x++) {
for (int i = 0; i <= amount; i++) {
Tuple tuple = new Tuple (coins);
table[x][i] = tuple;
}
}
for (int i = 1; i <= amount; i++) {
Tuple tuple = table[0][i];
int coin = Integer.parseInt(args[1]);
int total = i;
int remainder = total % coin;
int coinAmt= (int)Math.floor(total / coin);
if (remainder == 0) {
tuple.setElement(0, coinAmt);
}
}
for(int x = 1; x < coins; x++) {
int coin = Integer.parseInt(args[x + 1]);
for (int i = 1; i <= amount; i++) {
Tuple tuple = table[x][i];
int total = i;
int remainder = total % coin;
int coinAmt= (int)Math.floor(total / coin);
if (remainder == 0) {
tuple.setElement(x, coinAmt);
int optimalRow = optimalCheck(table, tuple, x, i);
Tuple optimalTuple = table[optimalRow][i];
tuple.copy(optimalTuple);
} else if (remainder != 0 && amount < coin) {
tuple.copy(table[x - 1][i]);
} else if (remainder != 0 && amount > coin) {
tuple.setElement(x, coinAmt);
Tuple remainderTuple = table[x][remainder];
tuple.add(remainderTuple);
int optimalRow = optimalCheck(table, tuple, x, i);
Tuple optimalTuple = table[optimalRow][i];
tuple.copy(optimalTuple);
}
}
}
Tuple finalAnswer = table[coins - 1][amount];
String result = "";
result = result + finalAnswer.total() + " COIN(S) IN TOTAL: ";
for (int i = 0; i < coins; i++) {
result = result + finalAnswer.getElement(i) + " x " + args[i + 1] + " cent , ";
}
if (finalAnswer.total() == 0) {
System.out.println("AMOUNT CANNOT BE MADE");
} else {
System.out.println(result);
}
}
public static int optimalCheck(Tuple[][] table, Tuple tuple, int row, int column) {
int total = tuple.total();
int optimalRow = 0;
for (int i = 0; i <= row; i++ ){
int checkedTotal = table[i][column].total();
if (checkedTotal < total && checkedTotal > 0) {
optimalRow = optimalRow + i;
break;
} else if (checkedTotal == total && i == row) {
optimalRow = optimalRow + row;
break;
}
}
return optimalRow;
} }
在大多数情况下,我的代码是正确的。唯一不对的是,它计算了作为面额的变化量,它削减了我的第一个面额金额。
所以,例如,如果我在命令行中放置1 5 10 25 133(1,5,10和25美分硬币以赚133美分),它将返回:
1 COIN(S) IN TOTAL: 0 x 5 cent , 0 x 10 cent , 0 x 25 cent , 1 x 133 cent ,
我查看了代码,但我不知道哪里出错了。谁能告诉我我在哪里犯了错误?非常感谢。
答案 0 :(得分:1)
在你的for循环中,你想要
for (int i = 1; i < coins; i++) {
不是
for (int i = 1; i <= coins; i++) {
<=
将包含coins
,即length - 1
- 最后一个参数。