杭电2069之Coin Change

来源:互联网 发布:java编码哪个是中文的 编辑:程序博客网 时间:2024/06/06 19:10
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
1126
 

Sample Output
4

13

分析:

暴力枚举算法,不过要考虑是否超时,需要缩小范围!!!

AC代码如下:

#include "iostream"using namespace std;int main(int argc, char* argv[]){    int i,j,k,l,m;    int n,count;    while(cin>>n)    {        count=0;        for (m=0;m<=5;m++)            for (l=0;l<=10-2*m;l++)                    for (k=0;k<=25-5*m;k++)                    for (j=0;j<=50-2*k;j++)                        for(i=0;i<=100;i++)                                        if (i*1+j*5+k*10+l*25+m*50==n&&i+j+k+l+m<=100)                            {                                count++;                            }        cout<<count<<endl;    }    return 0;}

0 0