https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=293
這是動態規化的問題題目限定範圍所以建立一個30001的陣列,一開始先把全部初始化1 然後再一個個幣值dp[]相加
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int coin[]={1,5,10,25,50};
long dp[]=new long [30001];
for(int i=0;i<30001;i++)
dp[i]=1;
for(int i=1;i<5;i++){
for(int j=coin[i];j<30001;j++){
dp[j]+=dp[j-coin[i]];
}
}
while(scn.hasNext()){
int num=scn.nextInt();
if(dp[num]>1)
System.out.printf("There are %d ways to produce %d cents change.\n",dp[num],num);
else
System.out.printf("There is only 1 way to produce %d cents change.\n",num);
}
}
/*
題目:Q357: Let Me Count The Ways
作者:1010
時間:西元 2016 年 8 月 */
}