HDU:2069 Coin Change

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 make changes with these coins for a given amount of money.

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

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

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

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

Sample Input
11
26

Sample Output
4
13

Author
Lily

Source

浙江工业大学网络选拔赛

solution:

#include<stdio.h>
int main()
{
    int coin[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()
{
    int a,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()
{
    int a,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()
{
    int a,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()
{
    int a[]={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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值