hdu 2069 Coin Change 解题报告

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069

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 coins.
 


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
本题不知道怎么说,开始用母函数做,一直 wa,与网上牛人打的表对比,发现前100的都是对的,原因是硬币不能超过100.
没办法就选择了暴力了, 然后还居然A了····
wa的代码:
#include <stdio.h>
#include
<stdlib.h>
#define Max 20000
int c1[Max], c2[Max], c3[Max], c4[Max], c5[Max];
int main()
{
int n;
while( scanf( "%d", &n ) != EOF )
{
for( int i=0; i<Max; ++i )
c5[i]
=c4[i]=c1[i]=c2[i]=c3[i]=0;
for( int i=0; i<=n; ++i ) //初始化系数为 1 ( 1+x + +x^2 + x^3·· )
{
c1[i]
=1;
}
for( int i=0; i<=n; ++i )
{
for( int j=0; i+j<=n; j+=5 )
{
c2[i
+j]+=c1[i];
}
}
for( int i=0; i<=n; ++i )
{
for( int j=0; i+j<=n; j+=10 )
{
c3[i
+j]+=c2[i];
}
}
for( int i=0; i<=n; ++i )
{
for( int j=0; i+j<=n; j+=25 )
{
c4[i
+j]+=c3[i];
}
}
for( int i=0; i<=n; ++i )
{
for( int j=0; i+j<=n; j+=50 )
{
c5[i
+j]+=c4[i];
}
}
printf(
"%d\n", c5[n] );
}
return 0;
}

  ac的代码:

#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(e%50==0&&a+b+c+d+e/50<=100)count++;
}
printf(
"%d\n",count);
}
}

  

原文地址:https://www.cnblogs.com/jian1573/p/2131317.html