HDU:2069 Coin Change

来源:互联网 发布:怎样退出淘宝客推广 编辑:程序博客网 时间:2024/04/29 15:44

Coin Change

Time Limit:1000/1000 MS(Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s):2461    Accepted Submission(s): 735


Problem Description
Suppose there are 5 types of coins:50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to makechanges with these coins for a given amount of money.

For example, if we have 11 cents, then we can make changes with one10-cent coin and one 1-cent coin, or two 5-cent coins and one1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven1-cent coins. So there are four ways of making changes for 11 centswith the above coins. Note that we count that there is one way ofmaking change for zero cent.

Write a program to find the total number of different ways ofmaking changes for any amount of money in cents. Your program should be able to handle up to 100cents.

Input
The input file contains any number oflines, each one consisting of a number ( ≤250 ) for the amount ofmoney in cents.

Output
For each input line, output a linecontaining the number of different ways of making changes with theabove 5 types of coins.

Sample Input
1126

Sample Output
413

Author
Lily

Source

浙江工业大学网络选拔赛

solution:

#include<stdio.h>
int main()
{
    intcoin[5]={1,5,10,25,50},i,j,k,n,a[251],b[251];
   while(scanf("%d",&n)!=EOF)
    {
       for(i=0;i<=n;i++)
       {a[i]=1;b[i]=0;}
       for(i=1;i<5;i++)
       {
           for(j=0;j<=n;j++)
               for(k=0;k+j<=n;k+=coin[i])
                   b[k+j]+=a[j];
               for(k=0;k<=n;k++)
               a[k]=b[k],b[k]=0;
       }

       printf("%d\n",a[n]);
    }
}

//以上使用生成函数做的,但是WRONG,原因在于出题者将红色部分错误表达为硬币总数不能超过100块

这样的话,我们就可以使用暴力解法,例如百钱买百鳮的问题

解法如下:

#include<stdio.h>
int main()
{
    inta,b,c,d,e,count,n;
   while(scanf("%d",&n)!=EOF)
    {
       count=0;
       for(a=0;a<=100;a++)
           for(b=0;5*b<=n;b++)
               for(c=0;10*c<=n;c++)
                   for(d=0;25*d<=n;d++)
                       for(e=0;50*e<=n;e++)
                       if(a+5*b+10*c+25*d+50*e==n&&a+b+c+d+e<=100)count++;
      printf("%d\n",count);
    }
}

13726292009-05-16 15:16:48Accepted2069828MS180K424 BCxiaotech

 

优化下:

#include<stdio.h>
int main()
{
    inta,b,c,d,e,count,n;
   while(scanf("%d",&n)!=EOF)
    {
       count=0;
       for(a=0;a<=100;a++)
           for(b=0;5*b<=n-a;b++)
               for(c=0;10*c<=n-a-5*b;c++)
                   for(d=0;25*d<=n-a-5*b-10*c;d++)
                       for(e=0;50*e<=n-a-5*b-10*c-25*d;e++)
                       if(a+5*b+10*c+25*d+50*e==n&&a+b+c+d+e<=100)count++;
      printf("%d\n",count);
    }
}

13726392009-05-16 15:20:26Accepted206993MS180K459 BCxiaotech

 

再优化下:

#include<stdio.h>
int main()
{
    inta,b,c,d,e,count,n;
   while(scanf("%d",&n)!=EOF)
    {
       count=0;
       for(a=0;a<=n;a++)
           for(b=0;5*b<=n-a;b++)
               for(c=0;10*c<=n-a-5*b;c++)
                   for(d=0;25*d<=n-a-5*b-10*c;d++)
                   {
                       e=n-a-5*b-10*c-25*d;
                       if(eP==0&&a+b+c+d+e/50<=100
)count++;
                   }
      printf("%d\n",count);
    }
}

13726762009-05-16 15:32:08Accepted206978MS180K474 BCxiaotech

 

目前水平只能优化到此,下面使用更暴力的办法,0ms

#include<stdio.h>
int main()
{
    inta[]={1,1,1,1,1,2,2,2,2,2,4,4,4,4,4,6,6,6,6,6,9,9,
   9,9,9,13,13,13,13,13,18,18,18,18,18,24,24,24,24,24,31,
   31,31,31,31,39,39,39,39,39,50,50,50,50,50,62,62,62,62,62,
   77,77,77,77,77,93,93,93,93,93,112,112,112,112,112,134,134,
   134,134,134,159,159,159,159,159,187,187,187,187,187,218,218,
   218,218,218,252,252,252,252,252,292,291,291,291,291,333,333,
   333,333,332,380,380,380,379,378,430,430,429,428,427,485,484,
   483,482,482,544,543,542,541,539,608,607,606,604,602,677,676,
   673,671,670,751,748,746,744,743,828,825,823,822,818,912,910,
   908,904,900,1001,999,995,990,986,1098,1093,1088,1084,1081,1196,
   1191,1186,1182,1177,1301,1296,1292,1285,1278,1413,1408,1400,
   1393,1387,1532,1524,1515,1508,1503,1654,1644,1637,1631,1622,1782,
   1773,1766,1757,1746,1916,1909,1898,1886,1875,2061,2049,2037,2025,
   2015,2208,2194,2181,2170,2156,2361,2348,2336,2321,2306,2521,2508,
   2492,2475,2459,2691,2673,2654,2637,2622,2863,2843,2824,2808,2789,
   3042,3021,3004,2983,2960,3228,3209,3187,3164,3140,3424,3401,3376,
   3351,3329,3623,3596,3570,3545,3517,3830,},n;
   while(scanf("%d",&n)!=EOF)
       printf("%d\n",a[n]);
}

13727012009-05-16 15:39:15Accepted20690MS180K1219 BCxiaotech