UVA-147 Dollars---完全背包+打表
题目链接:
https://vjudge.net/problem/UVA-147
题目大意:
给定11种面值分别为$100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and 5c coins的钱,现在给定一个钱数,求出可以组成的种类数,类似于uva 674,不过此题的精度太强了,纠结。。。
int n=(int)(nn*100+0.5);,注意用long long ,种类数可能非常大
用dp[i][j]表示用前i种硬币,组成j分的种类数,
状态转移方程:dp[i][j]+=DP(i-1,j-k*d[i]);
和UVA-674类似
1 #include<iostream> 2 #include<vector> 3 #include<queue> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdio> 7 #include<set> 8 #include<map> 9 #include<cmath> 10 #include<sstream> 11 using namespace std; 12 typedef pair<int, int> Pair; 13 typedef long long ll; 14 const int INF = 0x3f3f3f3f; 15 int T, n, m,d; 16 const int maxn = 3e4 + 10; 17 int a[] = {5,10,20,50,100,200,500,1000,2000,5000,10000}; 18 ll dp[maxn]; 19 //dp[i][j]表示前i种货币凑成价值j的种数 20 //dp[i][j] = dp[i - 1][j] + dp[i - 1][j - w[i]] 21 22 int main() 23 { 24 dp[0] = 1; 25 for(int i = 0; i < 11; i++) 26 for(int j = a[i]; j < maxn; j++) 27 dp[j] = dp[j] + dp[j - a[i]]; 28 double c; 29 while(cin >> c) 30 { 31 if(c == 0)break; 32 n = c * 100 + 0.5;//精度处理 33 printf("%6.2f%17lld\n", c, dp[n]); 34 } 35 return 0; 36 }
越努力,越幸运