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