国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 东宁县| 五大连池市| 黔江区| 上栗县| 香格里拉县| 咸阳市| 天镇县| 景谷| 青河县| 舟山市| 宁乡县| 普格县| 海丰县| 贵阳市| 应用必备| 将乐县| 平顺县| 黎川县| 安达市| 涟源市| 阜新市| 崇左市| 河间市| 泰兴市| 宜阳县| 城口县| 恭城| 抚远县| 策勒县| 雷波县| 霍林郭勒市| 高台县| 永寿县| 株洲县| 靖江市| 汉寿县| 台东县| 海安县| 丹江口市| 贞丰县| 获嘉县|