180 likes | 460 Views
Lecture 9 - Recursive. 1. Main Index. Contents. Recursive Case—Power Function Recursive Case - Hanoi Tower Recursive Case - Fibonacci Iterative Case - Fibonacci Recursive Case– Multibase Recursive Case-- Greatest Common Divisor. 2. Main Index. Contents.
E N D
Lecture 9 - Recursive 1 Main Index Contents • Recursive Case—Power Function • Recursive Case-Hanoi Tower • Recursive Case- Fibonacci • Iterative Case-Fibonacci • Recursive Case– Multibase • Recursive Case-- Greatest Common Divisor
2 Main Index Contents Recursive Definition of the Power Function • A recursive definition distinguishes between the exponent n = 0 (starting point) and n 1 which assumes we already know the value xn-1. • After determining a starting point, each step uses a known power of 2 and doubles it to compute the next result. • Using this process gives us a new definition for the power function, xn. • We compute all successive powers of x by multiplying the previous value by x.
Stopping Conditions for Recursive Algorithms • Use a recursive function to implement a recursive algorithm. • The design of a recursive function consists of 1. One or more stopping conditions that can be directly evaluated for certain arguments. 2. One or more recursive steps in which a current value of the function can be computed by repeated calling of the function with arguments that will eventually arrive at a stopping condition.
4 Main Index Contents Implementing the Recursive Power Function Recursive power(): double power(double x, int n) // n is a non-negative integer { if (n == 0) return 1.0; // stopping condition else return x * power(x,n-1); // recursive step }
5 Main Index Contents Solving the Tower of Hanoi Puzzle using Recursion
6 Main Index Contents Solving the Tower of Hanoi Puzzle using Recursion
7 Main Index Contents Solving the Tower of Hanoi Puzzle using Recursion
Hanoi using Recursion void hanoi(int n, const string& initNeedle, const string& endNeedle, const string& tempNeedle) { if(n==1)cout<<“move”<<initNeedle<< “to”<<endNeedle<<endl; else{ hanoi(n-1,initNeedle, tempNeedle, endNeedle); cout<<“move”<<initNeedle<<“to”<<endNeedle<<endl; hanoi(n-1,tempNeedle,endNeedle,initNeedle); } } 动态演示: (东北大学-高级语言程序设计) http://www.neu.edu.cn/cxsj/case/case11.html
Hanoi using Recursion Tk=2Tk-1+1 =2(2Tk-2+1)+1 =2(2(2Tk-3+1)+1)+1 …… =2k-1+2k-2+……+1 =2k-1
Hanoi using Recursion 在印度,有一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针。印度教的主神梵天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片,这就是所谓的汉诺塔。 不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金片:一次只移动一片,不管在哪根针上,小片必须在大片上面。僧侣们预言,当所有的金片都从梵天穿好的那根针上移到另外一根针上时,世界就将在一声霹雳中消灭,而梵塔、庙宇和众生也都将同归于尽。
Hanoi using Recursion 已知:Tk=2k-1 所以:f(64)= 264-1=18446744073709551615 假如每秒钟移动一个盘子,共需多长时间呢?一个平年365天有 31536000 秒,闰年366天有31622400秒,平均每年31556952秒,则: 18446744073709551615/31556952= 584554049253.855年 这表明移完这些金片需要5845亿年以上,而地球存在至今不过45亿年,太阳系的预期寿命约数百亿年。
Fibonacci Numbers using Recursion Int fib(int n) { if(n==1) return 1; if(n==2) return 1; else return fib(n-1)+fib(n-2); }
Fibonacci Numbers using Recursion Fib(5) Fib(4) Fib(3) Fib(3) Fib(2) Fib(2) Fib(1) Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0) Fib(1) Fib(0)
Fibonacci Numbers using Recursion 可推证: numCall(n)=2*fib(n+1)-1 numCall(5)=2*fib(6)-1=2*8-1=15 numCall(35)=2*fib(36)-1=2*1490352-1=29560703 调用次数过多,效率低
Fibonacci Numbers using Iteration int fibiter(int n) { // integers to store previous two // Fibonacci value int oneback = 1, twoback = 1, current; int i; // return is immediate for first two numbers if (n == 1 || n == 2) return 1;
Fibonacci Numbers using Iteration else // compute successive terms beginning at 3 for (i = 3; i <= n; i++) { current = oneback + twoback; twoback = oneback; // update for next calculation oneback = current; } return current; }
Multibase 1.Multibase output Void displayInBase(int n, int base) { if(n>0){ displayInBase(n/base,base); cout<<n%base;} } displayInBase(85,8) displayInBase(10,8) displayInBase(1,8) displayInBase(0,8) Output 85%8=5 output 10%8=2 output 1%8=1
The Greatest Common Divisor:最大公约数 2.Number Theory: The Greatest Common Divisor Int gcd(int a,int b) { if(b==0)return a; else return gcd(b, a%b); } Footnote: 欧几里德算法(Euclidean Algorithm)(Euclid‘s 算法)就是 通常所说的求最大公因数的辗转相除法。