Coin ChangeAC代码递归+详细的注释

Coin Change

 

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21645    Accepted Submission(s): 7577

 

 

 

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

 

 

 
代码及注释:
//附上详细的注释只是想让在看的你更加容易理解,不需要去句句斟酌浪费时间;
//写的很仓促,有写的不好的地方还请包涵谢谢
#include<iostream>
using namespace std;
int coin[]={50,25,10,5,1};
int countt;//累计种数
int account=0;//输入金币
void change(int num,int j,int coinnum)
{
    if(num==account&&coinnum<=100)//累计和num等于输入金额&&硬币个数小于等于100个,计数
        countt++;
    else if(num>account||coinnum>100)//累计和num大于输入金额||硬币个数大于100个,结束
        return;
    else//累计和num小于输入金额,继续递归
    for(int i=j;i<5;i++)
    {
        j=i;coinnum+=1;//这里加了个j是因为coin[0-->j]面值的硬币在前面已经算过了,比如:account=50;第一次递归:j=i=0;coinnum=0+1;
        change(num+coin[i],j,coinnum);//change(0+50,0,1);第二次递归开始:if(num==account&&coinnum<=100) 满足,计数,结束
        coinnum-=1;                   //coinnum==coinnum-1==1-1==0;i++,i=1;j=1,即去掉50;coinnum=1;change(25,1,1)第三次递归开始:
    }                                 //25小于50,继续递归;i=j=1(设置j用以就在此,不然如果i从0开始会重复计数)j=1;coinnum=1+1=2;
                                    //chang(25+25,1,2);第四次递归开始:if(num==account&&coinnum<=100) 满足,计数,结束
                                    //*
                                    //*
                                    //*
}                                   //如果没有j,像(25,10,10,5),(10,25,10,5},{5,25,10,10}会重复
int main()
{
    account=0;
    while(cin>>account)
       {
        countt=0;
        change(0,0,0);
        cout<<countt<<endl;
        account++;
       }
    return 0;
}
觉得有用的话就赞一个吧亲
ps:第一次在csdn上写文章
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值