题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=293
Dynamic programming
注意overflow。
代码如下:
9 #include <iostream> 10 #include <math.h> 11 #include <stdio.h> 12 #include <cstdio> 13 #include <algorithm> 14 #include <string.h> 15 #include <cstring> 16 #include <queue> 17 #include <vector> 18 #include <functional> 19 #include <cmath> 20 #define SCF(a) scanf("%d", &a) 21 #define IN(a) cin>>a 22 #define FOR(i, a, b) for(int i=a;i<b;i++) 23 typedef long long Int; 24 using namespace std; 25 26 int main() 27 { 28 int n; 29 int type[5] = { 1, 5, 10, 25, 50 }; 30 Int ways[30005] = { 0 }; 31 ways[0] = 1; 32 FOR(i, 0, 5) 33 { 34 FOR(j, type[i], 30005) 35 { 36 ways[j] += ways[j - type[i]]; 37 } 38 } 39 40 while (SCF(n)!=EOF) 41 { 42 if (ways[n] == 1) 43 printf("There is only 1 way to produce %d cents change.\n", n); 44 else 45 printf("There are %lld ways to produce %d cents change.\n", ways[n], n); 46 } 47 return 0; 48 }