Coin change && climb stairs && Monkey pick Banana

Source: Internet
Author: User

Coin change && climb stairs && Monkey pick Banana

Suppose there are several coins, such as 1, 3, 5, and the number is infinite. Please find out the minimum number of coins that can be used to make up a certain amount of change.

  
 
  1.      
        
    1. #include"CoinProblem.h"
    2. #include<iostream>
    3. int countNum=MAX;
    4. void CoinProblem(int *coin,int Length,int Value,int count){
    5. if(Value==0){
    6. if(countNum>count){
    7. countNum=count;
    8. }
    9. return;
    10. }
    11. for(int i=0;i<Length; i++){
    12. if(Value>=coin[i]){
    13. CoinProblem(coin,Length,Value-coin[i],count+1);
    14. }
    15. }
    16. }
         
        
    1. #ifndef COIN_PROBLEM_H
    2. #define COIN_PROBLEM_H
    3. #define MAX 65535
    4. extern int countNum;
    5. void CoinProblem(int *coin,int Length,int Value,int count);
    6. #endif
         
        
    1. #include"CoinProblem.h"
    2. #include<iostream>
    3. int main(){
    4. int coin[3]={1,3,5};
    5. CoinProblem(coin,3,5,0);
    6. std::cout<<countNum<<std::endl;
    7. }


These questions are a kind of problem, you monkey pick banana, coin change, climb stairs and so on.

The common denominator of this type of problem is that you want to solve the problem, that is, you have to just ask is not much to solve, no matter how you pick bananas, no matter how you

Pick a few, you have to finish the banana. You have to find someone else that money, not more and less. You can climb the stairs, too. The inverse is to solve the problem.

This is not like a knapsack problem, because the backpack is not necessarily full, that is, the end condition is uncertain.

But we don't care if it happens, because we use the ladder-return. Because the benefit of recursion is to consider all the issues that can be considered, including just solving the problem and

Ask for more, or less, of the problem.

  
 
  1.      
        
    1. if(Value==0){
    2. if(countNum>count){
    3. countNum=count;
    4. }
    5. return;
    6. }


As the code above is specifically designed to limit the end condition, we count only when we find the exact amount of money.

So we may limit the cases to be counted by our own qualifications.

Special attention is paid to:

? Because of my own negligence, which led to some minor problems in writing the code before, I wrote it before.

  
 
  1.      
        
    1. void CoinProblem(int *coin,int Length,int Value,int count){
    2. if(Value==0){
    3. if(countNum>count){
    4. countNum=count;
    5. }
    6. return;
    7. }
    8. for(int i=0;i<Length; i++){
    9. if(Value>=coin[i]){
    10. CoinProblem(coin,Length,Value-coin[i],++count);
    11. }
    12. }
    13. }

Look at the red part of the code, here is ++count, here is the value of change count, I mean to change the value of all count in this recursive operation, that is, i=0

such as count=2, but also meet the conditions, that is, to enter the conditional judgment statement, so use ++count will find that count becomes 3, which is really what we expect, but

If the next time "return" back, I=1, then found that count at the beginning is 3. is no longer the 2 we expected.

So the correct wording should be this:

  
 
  1.      
        
    1. void CoinProblem(int *coin,int Length,int Value,int count){
    2. if(Value==0){
    3. if(countNum>count){
    4. countNum=count;
    5. }
    6. return;
    7. }
    8. for(int i=0;i<Length; i++){
    9. if(Value>=coin[i]){
    10. CoinProblem(coin,Length,Value-coin[i],count+1);
    11. }
    12. }
    13. }



From for notes (Wiz)

Coin change && climb stairs && Monkey pick Banana

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.