Implementation of recursive algorithms C ++ and PHP for data structures and algorithms

Source: Internet
Author: User
Data structures and algorithms: Recursive Algorithms C ++ and PHP are the basis for algorithm implementation. algorithms are always implemented based on a certain data structure. When developing an algorithm, we often construct a data structure suitable for this algorithm. If a data structure is separated from an algorithm, there will be no value.

Functions of algorithms ----Solving any practical problem inevitably involves the issue of algorithms. a certain algorithm is used to obtain an optimal (or optimal) solution.

Recursive algorithms: a simple algorithm that uses known conditions to draw intermediate inferences from specific relationships until the results are obtained.

Forward Method: based on known conditions, we can gradually calculate the problem to be solved.

Inverse Method: based on the results of known problems, iterative expressions are used to calculate the conditions for the start of a problem, that is, the inverse process of the forward method.

Push instance:

Rabbit breeding process


C ++ code:

# Include
 
  
Int main () {using namespace std; const int NUM = 13; int count = 0; int rabbit [NUM] = {1, 1}; for (int I = 0; I
    
   

Php code:

    ";}?>

C ++ compilation and running results



Reverse push instance:

My father is planning to save a sum of money for the four-year college life of Xiaolong. he uses the full-storage method and controls Xiaolong to get 1000 yuan at the end of each month for use next month. Assume that the annual interest rate received by the bank is 1.71%, calculate the minimum amount of money that the father needs to deposit.

C ++ code:

# Include
    
     
Int main () {using namespace std; const double RATE = 0.0171; double money [48]; money [47] = 1000; for (int I = 47; I> 0; I --) {money [i-1] = (money [I] + 1000)/(1 + RATE/12);} for (int j = 47; j> 0; j --) {cout <"no." <j <"total monthly profits:" <money [j] <"yuan \ n ";} return 0 ;}
    

Php code:

    0; $ I --) {$ month [$ i-1] = ($ month [$ I] + 1000)/(1 + RATE/12 );} for ($ I = 47; $ I> 0; $ I --) {echo "no ". $ I. "Total monthly information :". $ month [$ I]. "Yuan
";}?>

C ++ compilation and running results

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.