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
上面是ac代码,下面是wa代码,感觉有太坑了,网上不少人跟我一样,上来直接用母函数直接交
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a,b,c,d,e,n,cnt;
int main()
{
while (~scanf("%d",&n))
{
cnt=0;
for (a=0;a<=n;a++)
for (b=0;b*5<=n-a;b++)
for (c=0;c*10<=n-a-b*5;c++)
for (d=0;d*25<=n-a-b*5-c*10;d++)
{
e=n-a-b*5-c*10-d*25;
if (e%50==0&&a+b+c+d+e/50<=100)
cnt++;
}
printf("%d\n",cnt);
}
return 0;
}
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
int a[10],c1[300],c2[300];
int main()
{
int n;
a[1]=1;a[2]=5;a[3]=10;a[4]=25;a[5]=50;
while (~scanf("%d",&n))
{
int i,j,k;
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
c1[0]=1;
for (i=1;i<=5;i++)
{
for (j=0;j<=n;j++)
for (k=0;k+j<=n;k+=a[i])
c2[j+k]+=c1[j];
for (j=0;j<=n;j++)
{
c1[j]=c2[j];
c2[j]=0;
}
}
printf("%d\n",c1[n]);
}
return 0;
}