Thinking behind the problem of finding change in the tool

Source: Internet
Author: User
For more information, refer to the questions in SiCp P26.

The Recursive Method for this problem is very simple, similar to the thought of a backpack.

That is, the amount of cash for amount is replaced with N types of coins to meet the cycle unchanged: count_change (amount, n) = count_change (amount, n-1) + count_change (amount-amount_of_first_coin, n) recursive stop condition: When a = 0, the result is 1 A <0, and the result is 0. When n = 0, the result is also 0.
  • Convert the preceding rule to scheme code and run it in drracket.
 1      #lang racket 2      (define (count-change amount kinds-of-coins)  3           ( cond ((= amount 0) 1) 4          ((or (< amount 0) (= kinds-of-coins 0))0) 5          (else  6            (+(count-change amount (- kinds-of-coins 1)) (count-change (- amount (some-coin kinds-of-coins)) kinds-of-coins )) ) ) ) 7    8      (define (some-coin kinds-of-coins) 9        ( cond ((= kinds-of-coins 1) 1)10          ((= kinds-of-coins 2) 5)11          ((= kinds-of-coins 3) 10)12          ((= kinds-of-coins 4) 25)13          ((= kinds-of-coins 5) 50)))14 15   (count-change 45 5)

 


  • The above code is very slow when it solves amount = 300 or more, because it is a recursion, not a tail recursion (iteration), where a large number of repetitive and redundant computations exist, but this problem is more complicated than the Fibonacci number, because
The Fibonacci number problem is easily converted into iteration because it is of a good nature and only 2 branches each time. Each problem directly satisfies the nature of the best self-problem. However, if the change is not the case, it cannot be simply converted to tail recursion. In addition to converting recursion into iteration, another compromise optimization technique is dynamic planning. The idea is as follows: coin = [C1, C2, C3…] [Change The amount of amount coins to kind_of_coins type change quantity] = [only one type of change C1 change quantity] + [use two types of change C1 C2] + ...... We can see that [the number of C1 C2 types with two kinds of change] is the same processing for the second C2.
 1  //cpp 2           #include <iostream> 3           #include <vector> 4           using namespace std; 5  6           int main () 7           {      8                  int amount = 55; 9                  const int kind_of_coins = 5 ;10                  int coin [ kind_of_coins] = { 1 , 5 , 10, 25 , 50 };11                  vector< int > result ( amount + 1 , 0 );12                  result [0 ] = 1;13       14                  for (int i = 0 ; i < kind_of_coins; ++ i ){15                        int j = coin[ i ];16                        for (; j <= amount; ++ j )17                              result [j ] += result[ j - coin [ i]];18                   }19 20               cout << result [amount ] << endl;21               system ("pause" );22       23           }24  
Use Python:
 1  //cpp 2           #include <iostream> 3           #include <vector> 4           using namespace std; 5  6           int main () 7           {      8                  int amount = 55; 9                  const int kind_of_coins = 5 ;10                  int coin [ kind_of_coins] = { 1 , 5 , 10, 25 , 50 };11                  vector< int > result ( amount + 1 , 0 );12                  result [0 ] = 1;13       14                  for (int i = 0 ; i < kind_of_coins; ++ i ){15                        int j = coin[ i ];16                        for (; j <= amount; ++ j )17                              result [j ] += result[ j - coin [ i]];18                   }19 20               cout << result [amount ] << endl;21               system ("pause" );22       23           }

 

Recursion and iteration are problems that arise from the beginning of the programming language. Then, we will repeat the process of understanding, discovering new content, and understanding again. Whether C ++ or Python, The Syntax provides a loop structure, and the displayed loop structure is essentially a tail recursion mode, which is a characterization of iteration, tail recursion is more likely to reflect the meaning of iteration than loops. However, these C-family languages do not support tail recursion, so there are two possibilities for writing tail recursion: first, the compiler optimizes it into a loop. Second, the compiler treats it as a normal recursion, and there is a lot of redundant computing. Therefore, we generally optimize recursion into loops. It is easy to create an illusion that loops are efficient, while recursion is inefficient. Let's take a look at scheme, a dialect of Lisp. The syntax supports tail recursion to implement iteration. For the two versions generated by the Fibonacci number:

# Lang racket

Recursive version
(Define (maid N)
(Cond (= N 0) 0)
(= N 1) 1)
(Else (+ (maid (-N 1) (maid (-N 2 ))))))

 

Iterative version

# Lang racket
(Define (maid N)
(FIB 0 1 N ))
(Define (FIB first-number second-number N)
(If (= N 0)

First-Number
(FIB second-number (+ first-number second-Number) (-N 1 ))))

It is easy to find that iteration is much faster than recursion, because it does not have redundant repeated calculations, and more importantly, it maintains constant space each time, without the need to expand and contract first, remember the path of the operation. In addition to iteration, the general optimization method also involves manual stack simulation or dynamic planning. Stack simulation has nothing to say, and recursive stacks are constructed manually. Dynamic Planning is actually very simple, that is, when we cannot convert the recursive process into tail recursion or iteration, we manually record the return values of the sub-process, when computing is a large process, it can be directly implemented. The disadvantage is that a relatively large space needs to be maintained, which is a typical space for time change. However, this is worthwhile and the storage is cheap to a certain extent, time efficiency is more valuable (of course there are limits, if the meaning of the infinite algorithm is small ).

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.