UVA 147 Dollars ( 完全背包+求解方案数精度问题)

来源:互联网 发布:释行宇谭腿 知乎 编辑:程序博客网 时间:2024/05/17 22:54

UVA   147    Dollars



11种金币,输出能组成给定数额的方法个数。注意精度问题

#include <cstdio>  #include <cstring>    const int N = 11;  const int maxn = 30005;  const int c[N] = {5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000};   //硬币分值 long long f[maxn];    int main () {      float num;      int n;      while (1) {          scanf ("%f", &num);          n = (num + 0.005) * 100;    //进行数值处理,转换为整数         memset (f, 0, sizeof (f));   //初始化 f[0] = 1;          for (int i = 0; i < N; i++) {             for (int j = c[i]; j <= maxn - 5; j++)              {                  f[j] += f[j - c[i]];              }}                     if (!n)              break;  //遇零跳出         printf ("%6.2f%17lld\n", num, f[n]);    //注意输出格式     }      return 0;  }  


0 0
原创粉丝点击