UVA 147 Dollars Problem(动态规划 硬币)
Dollars |
New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and 5c coins. Write a program that will determine, for any given amount, in how many ways that amount may be made up. Changing the order of listing does not increase the count. Thus 20c may be made up in 4 ways: 1 20c, 2 10c, 10c+2 5c, and 4 5c.
Input
Input will consist of a series of real numbers no greater than $300.00 each on a separate line. Each amount will be valid, that is will be a multiple of 5c. The file will be terminated by a line containing zero (0.00).
Output
Output will consist of a line for each of the amounts in the input, each line consisting of the amount of money (with two decimal places and right justified in a field of width 6), followed by the number of ways in which that amount may be made up, right justified in a field of width 17.
Sample input
0.20 2.00 0.00
Sample output
0.20 4 2.00 293
这一道跟前面两个硬币问题一个类型的,需要注意的是精度和格式:int x=(int)(n*100+0.5);double转化成int类型时四舍五入;输出%6.2f,这个6表示包含小数点及小数点后面的两位一共占6个字符
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 # define maxn 30002 6 7 long long num[maxn]; 8 long long cent[12]={0,5,10,20,50,100,200,500,1000,2000,5000,10000}; 9 10 int main() 11 { 12 int i,t; 13 double n; 14 for( i=1;i<=maxn;i++) 15 num[i]=0; 16 num[0]=1; 17 18 for(t=1;t<=11;t++) 19 for(i=1;i<=maxn;i++) 20 { 21 if(i>=cent[t]) num[i]+=num[i-cent[t]];// 状态转移方程 22 } 23 while(scanf("%lf",&n)!=EOF) 24 { 25 int x=(int)(n*100+0.5); 26 if(x==0) 27 break; 28 printf("%6.2f%17lld\n",n,num[x]); 29 } 30 return 0; 31 }