遞迴函式的使用

2021-08-10 16:09:48 字數 807 閱讀 4782

#if 0

有 5 個人坐在一起, 問第 5 個人多少歲? 他說比第 4 個人大 2 歲。 問第 4 個人歲

數, 他說比第 3 個人大 3 歲。 問第 3 個人, 又說比第 2 個人大 2 歲。 問第 2 個人, 說

比第 1 個人大 2 歲。 最後問第 1 個人, 他說是 10 歲。 請問第 5 個人多大?

#endif

#include "stdafx.h"

int getage(int n)

else }

int _tmain(int argc, _tchar* argv)

#if 0

猴子第一天摘下若干個桃子, 當即吃了一半, 還不過癮, 又多吃了乙個。 第二天

早上又將剩下的桃子吃掉一半, 又多吃了乙個。 以後每天早上都吃了前一天剩下的一

半零乙個。 到第 10 天只剩下乙個桃子了。 求第一天共摘了多少?

#endif

#include "stdafx.h"

int peach(int day)

else }

int _tmain(int argc, _tchar* argv)

#include "stdafx.h"

int factorial(int n)

else }

int _tmain(int argc, _tchar* argv)

遞迴返回 func(遞迴條件)

python中遞迴函式的使用

遞迴函式就是自己呼叫自己的函式,遞迴函式使用過程中應注意呼叫深度,否則計算機無法承受。1.使用遞迴函式實現計算某個數的階乘。def fact n if n 1 return 1return n fact n 1 print fact n 其中n為某個數。1.2用遞迴函式來實現獲取斐波拉契數列中的第n...

php遞迴使用示例 php遞迴函式

遞迴獲得角色id字串 function exploderole roleobj,resultstr else exploderole childroleobj,resultstr 遞迴獲取級聯角色資訊陣列 function makerolerelation roleobjarr 程式設計客棧 通過父...

js 遞迴函式的使用及常用函式

1.遞迴函式的使用 公園裡有一堆桃子,猴子每天吃掉一半,挑出乙個壞的扔掉,第6天的時候發現還剩1個桃子,問原來有多少個桃子 var peache function peaches n else return peache console.log peaches 0 es6 寫法 let peache...