zoukankan      html  css  js  c++  java
  • uva 147 Dollars

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83

    147 - Dollars

    Time limit: 3.000 seconds

    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 tex2html_wrap_inline25 20c, 2 tex2html_wrap_inline2510c, 10c+2 tex2html_wrap_inline25 5c, and 4 tex2html_wrap_inline25 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

    分析:

    这个题跟UVA674题类似,只是这个题有更多的钱的种类,并且需要用long long来保存

    做的方法是先乘以100消除浮点数的误差,然后计算,需要注意的是输出格式有要求,有特殊的空格要求~

    AC代码:

    递推:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 using namespace std;
     5 const int maxn=30001;
     6 int num[12]={0,5,10,20,50,100,200,500,1000,2000,5000,10000};
     7 int a,b;
     8 long long f[maxn][12];
     9 void Init()
    10 {
    11     for(int i=0;i<12;i++)
    12     f[0][i]=1;
    13     for(int i=1;i<maxn;i++)
    14     for(int j=1;j<12;j++)
    15         for(int k=0;num[j]*k<=i;k++)
    16         f[i][j]+=f[i-num[j]*k][j-1];
    17 }
    18 int main()
    19 {
    20     Init();
    21     while(scanf("%d.%d",&a,&b)&&(a+b))
    22     {
    23     int n=a*100+b;
    24     printf("%6.2lf%17lld
    ",n*1.0/100,f[n][11]);
    25     }
    26     return 0;
    27 }
    View Code

    背包:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 using namespace std;
     5 const int maxn=30001;
     6 int num[12]={0,5,10,20,50,100,200,500,1000,2000,5000,10000};
     7 int a,b;
     8 long long f[maxn][12];
     9 void Init()
    10 {
    11     for(int i=0;i<12;i++)
    12     f[0][i]=1;
    13     for(int i=1;i<maxn;i++)
    14     for(int j=1;j<12;j++)
    15         for(int k=0;num[j]*k<=i;k++)
    16         f[i][j]+=f[i-num[j]*k][j-1];
    17 }
    18 int main()
    19 {
    20     Init();
    21     while(scanf("%d.%d",&a,&b)&&(a+b))
    22     {
    23     int n=a*100+b;
    24     printf("%6.2lf%17lld
    ",n*1.0/100,f[n][11]);
    25     }
    26     return 0;
    27 }
    View Code

    后来想到可以再除以5(题目说是5的倍数),来减少运算量,提高运算效率。

     1 #include <cstdio>
     2 using namespace std;
     3 
     4 long long f[30010];
     5 const int num[] = {2000, 1000, 400, 200, 100, 40, 20, 10, 4, 2, 1};
     6 
     7 int main()
     8 {
     9     f[0] = 1;
    10     for(int i = 0 ;i <= 10;i ++)
    11         for(int j = num[i] ;j <= 6005;j ++)
    12             f[j] += f[j - num[i]];
    13     double x;
    14     while(scanf("%lf", &x) , x)
    15     {
    16         double tx = x * 20;
    17         printf("%6.2lf%17lld
    ", x , f[(int)tx] );
    18     }
    19     return 0;
    20 }
  • 相关阅读:
    JAVAWEB使用保存cookie、删除cookie、获取cookie工具类
    JAVA比较指定的两个日期
    编写一个C程序运行时输出 Hello World!
    正确理解以下名词及其含义:1源程序,目标程序,可执行程序2程序编辑,程序编译,程序连接3程序,程序模块,程序文件4函数,主函数,被调用函数,库函数5程序调试,程序测试
    为什么需要计算机语言?高级语言有哪些特点?
    什么是程序?什么是程序设计?
    题解 卡农
    题解 GT考试
    题解 SP6779 【GSS7
    题解 Crash 的文明世界
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4478884.html
Copyright © 2011-2022 走看看