用java编程:1元钱用1分,2分,5分,1角,2角,5角 ,1元有几种组合方法

2024-11-02 14:18:39
推荐回答(1个)
回答1:

public class CoinChange {

public static void main(String[] args){
int[] coins = {100,50,20,10,5,2,1};
int[][] change = new int[100][7];
for(int i=0;i<7;i++) change[0][i] = 1;

for(int i=1;i<100;i++){
for(int j=0;j<7;j++){
int previous = 0;
if(j>1) previous = change[i][j-1];
int newChange = 0;
if(coins[j]<=i) newChange = change[(i-coins[j])][j];
change[i][j] = previous+newChange;
}
}
System.out.println("the final result is "+change[99][6]);
}
}