国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Recursion

2019-11-08 02:53:25
字體:
供稿:網(wǎng)友

1. Introduction Any function which calls itself is called recursive. A recursive method solves a PRoblem by calling a copy of itself to work on a smaller problem. It is important that the recursion terminates. Each time the function calls itself with s slightly simpler version of the original problem. The results of smaller problems must converge back on the base case.

2. General format

if(test for base case) return base case valueelse if(another base case) return other base case value//The recursive caseelse return (some work and then a recursive call)

Example: To calculate factorial using recursive function:

int Factorial(int n){ if(n==0||n==1){ return 1; } else{ return n*Factorial(n-1); }}

3. Problems and solutions

1. Towers of Hanoi

int Count=0;void Move(int n, char from, char middle, char to){ if(n==1){ cout<<"move "<<from<<" to "<<to<<endl; Count++; } else{ Move(n-1,from,to,middle); cout<<"move "<<from<<" to "<<to<<endl; Count++; Move(n-1,middle,from,to); }}

2. Check of sorted array

bool Check(int a[],int n){ if(n==1||n==0){ return true; } else if(a[n-1]<a[n-2]){ return false; } else{ return Check(a, n-1); }}

3. Fibonacci Sequence

int Fib(int a){ if(a==1||a==2){ return 1; } else{ return Fib(a-1)+Fib(a-2); }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鸡泽县| 福泉市| 九台市| 兖州市| 松滋市| 阳春市| 鲁山县| 西乌珠穆沁旗| 夏河县| 监利县| 汕尾市| 区。| 汶上县| 内黄县| 新建县| 宁乡县| 伊川县| 海伦市| 五河县| 原平市| 寿阳县| 时尚| 遂川县| 高台县| 东安县| 家居| 铜鼓县| 博罗县| 玛曲县| 清涧县| 伊宁县| 富蕴县| 平罗县| 汾西县| 台北市| 淳化县| 福州市| 合江县| 大洼县| 会昌县| 新丰县|