1、基础算法递推法Recursive algorithm基础算法-递推法(Recursive algorithm)There is a class of questions, the changes between every two adjacent items have a certain regularity, we can put this rule into simple recursive formula as follows:Fn=g (Fn-1)This is the number in the sequence, establish the relationship betwee
2、n the antecedent and consequent, then from the initial condition (or end) to start, step by step according to the recursive relation of recursion, until the final results obtained (or initial value). Many programs are solved in this way. If a test, if we can find a relationship before and after a cl
3、ear and its initial condition (end result), like to solve the problem, let the computer calculation is a step by step, let the computer do the high-speed repetitive operation, can really play the best use effect.Recursive, backward push and push two forms. General analysis idea:If solving condition
4、F1Then begininvertedThe problem (or recursive relation) to determine the final result of Fa;For inverted formula Fi-1=g(Fi);I=n start from the final result FnWhile current results Fi non initial values F1 do are inverted by Fi-1=g (F1);Output push back results F1 and push backward process;End thenEl
5、se, begin, push,The problem (or forward relationship) to determine the initial value of F1 (boundary conditions);Find the forward relation formula F1=g (Fi-1);I=1; proceed from the boundary condition F1 The results of Fi while do Fn non final results by Fi=g (Fi-1) CIS push back;The output of the Fn
6、 results and the pushing process;End; elseI. backward pushing methodThe push down method, is not in the initial value of the case, by some recursive relations and informed the solution of the problem or goal, and then push down over, infer its initial conditions. Because the operations of such probl
7、ems are mapped one by one, the recurrence formula can be analyzed. Then, from this solution or goal, take the push back section, step by step into the initial statement of the problem.Here are some examples.1 oil storage pointA heavy truck wants to cross 1000 kilometers of desert, and the truck uses
8、 1 liters / km of fuel, and the total capacity of the truck is 500 liters. Obviously, a truck never gets through the desert at one time. As a result, drivers must try to set up several storage points along the way so that trucks can cross the desert smoothly. How can drivers build these oil storage
9、points? How much oil should be stored at each point to make the truck pass through the desert at the expense of least oil?Algorithm analysis:Oil storage point number calculation program and print the establishment, the oil storage from the edge of the desert of the distance and the storage of oil.No
10、. Distance (K.M.) oil (litre)1, X, X, X, X2, X, X, X, X3, X, X, X, X.Set disi as the distance between the I oil storage point and the end point (i=0);Oili is the storage quantity of the I oil storage points;We can solve this problem by the back pushing method. From the end to the beginning of the pu
11、sh down, one by one to find the location of each oil storage point and the amount of oil storage.But when the return point are shown:The strategy of pushing back from the oil storage point I to the oil storage point i+1 is that the truck moves back and forth between point I and point i+1. Each time
12、the truck returns to i+1, it runs out of 500 liters of gasoline, and each time it leaves the i+1, it has to hold 500 liters of gasoline. The distance between the two points must satisfy the I point i*500 gasoline fuel storage at least the requirements (0=i=1000;Disk: =1000; starting point to end dis
13、tance valueD1:=1000-disk-1 = i=n to start pointOilk: =d1* (2*k+1) +oilk-1; start point oil storageFor i:=0 to k do start from the start point, print the starting point to the distance from the current oil storage point and the amount of oil storageWriteln (I, 1000-disk-i: 30, oilk-i: 80);End. mainCo
14、nverts to the C language program as follows:#includeVoid, main ()Int k; / * * / oil storage location numberFloat D, D1 /*d:; the cumulative end point to the current oil storage point distance, d1:i=n distance to the starting point.Float, oil10, dis10;Int i;Printf (NO. distance (K.M.) toil (L.) n);K=
15、1;D=500; / * * / starting point began to push down from i=1Dis1=500;Oil1=500;DoK=k+1;D=d+500/ (2*k-1);Disk=d;Oilk=oilk-1+500;while (d=1000);Disk=1000; / * the start point to the end point of the distance.D1=1000-disk-1; / * for i=n to the starting point of the distance.Oilk=d1* (2*k+1) +oilk-1; / *
16、for starting point of oil reservoir.For (i=0; ik; i+) / * starting from the start point to the starting point to print the current oil storage point distance and the amount of oil reservoir.Printf (%dt%ft%ftn, I, 1000-disk-i, oilk-i);Practical algorithm (basic algorithm - recursive method -02)Date o
17、f issue: April 10, 2003 provenance: analysis and programming of practical algorithms. Author: C language home collation, 1317 readers have read this articleForward pushing methodThe reverse process of the push down method is pushed along, starting from the boundary conditions, the recursive relation
18、s launched by the latter value, value according to the recursive formula of re launch, followed by consequent value. recursive, until the problem from the initial statement to move forward to the solution of this problem.Real number sequence: a real number series with N items knownAi= (ai-1-ai+1) /2
19、+d, (1iN) (N60)The keyboard inputs are N, D, A1, an, m, and output amNo error is required for input data.Algorithm analysis:Analysis of the problem, the formula:Ai= (Ai-1-Ai+1) /2+d (1iN) (n=2) can be calculated:S32= (0,0,1) =S21;S33= (1,0,0) =S22;S34= (-2,2,1) =S23;S35= (5, -2-2) =S24;And.S3i= (.) =S2i-1;.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1