Coin Change
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.
11 26
4 13
#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++) //1
for(b=0;5*b<=n;b++) //5
for(c=0;10*c<=n;c++) //10
for(d=0;25*d<=n;d++) //25
for(e=0;50*e<=n;e++) //50
if(a+5*b+10*c+25*d+50*e==n&&a+b+c+d+e<=100)count++;
printf("%d\n",count);
}
}
//优化一下
#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);
}
}