UVa 357 - Let Me Count The Ways

又是硬币型的。。

虽然通过了,还有几个疑问:

1. 题目中说

  The input will consist of a set of numbers between 0 and 30000 inclusive, one per line in the input file. ,

   难道不包括30000吗? 输入30000时AC的程序结果是负值(将long long int 改为unsigned long long int可以解决)。

2. 使用dev c++输出时出现错误:

  There are 6 ways to produce 0 cents change.
  There are 4 ways to produce 0 cents change.

   怀疑是编译器造成的,尚无确定结论。

 1 /* 357 - Let Me Count The Ways */
2 # include <stdio.h>
3
4 const int coin[] = {0,1,5,10,25,50};
5
6 long long int f[30005][6];
7
8 long long dp(int m, int i);
9
10 int main()
11 {
12 int n;
13 long long int t;
14
15 while (~scanf("%d", &n))
16 {
17 t = dp(n, 5);
18 if (t == 1) printf("There is only 1 way to produce %d cents change.\n", n);
19 else printf("There are %lld ways to produce %d cents change.\n", t, n);
20 }
21
22 return 0;
23 }
24
25 long long int dp(int m, int i)
26 {
27 int k;
28 if (f[m][i] > 0) return f[m][i];
29 if (m == 0 || i <= 1) return f[m][i] = 1;
30 for (k = 0; k <= m; k += coin[i])
31 f[m][i] += dp(m-k, i-1);
32 return f[m][i];
33 }



posted on 2012-04-02 13:18  getgoing  阅读(351)  评论(0编辑  收藏  举报

导航