1.使用递归解决计算第N项斐波那切数列
斐波那契数列:F(0)=1,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*)
1,1,2,3,5,8,13,21,34,55,89...
代码:
#include<stdio.h> int func_f(int n) { if(1 == n || 2 == n) return 1; return func_f(n-1) + func_f(n-2); } int main(int argc,const char* argv[]) { printf("%d \n",func_f(5)); }
2.使用递归模拟N层汉诺塔的移动过程
#include<stdio.h> //s起点 m中间点 e终点 void hanio(int n,char s,char m, char e) { if(1 == n) { printf("%d from %c to %c\n",n,s,e); return; } hanio(n-1,s,e,m); printf("%d from %c to %c\n",n,s,e); hanio(n-1,m,s,e); } int main(int argc,const char* argv[]) { hanio(4,'A','B','C'); }
标签:练习,递归,int,编程,char,func,printf,hanio From: https://www.cnblogs.com/ljf-0804/p/17587913.html