例1:阶乘函数
#include<iostream>
using namespace std;
int f(int n)
{if(n==0)return 1;elsereturn f(n-1)*n;
}
int main()
{int n;cin>>n;cout<<f(n);return 0;
}
例2:Fibonacci数列
无穷数列1,1,2,3,5,8,13.....被称为Fibonacci数列
#include<iostream>
using namespace std;
int f(int n)
{if(n==0||n==1)return 1;elsereturn f(n-1)+f(n-2);
}
int main()
{int n;cin>>n;cout<<f(n-1);//求斐波那契数列的第n个数return 0;
}
例3:Ackerman函数
#include<iostream>
using namespace std;
int a(int n,int m)
{if(n==1&&m==0)return 2;else if(n==0)return 1;else if(m==0)return n+2;elsereturn a(a(n-1,m),m-1);
}
int main()
{int n,m;cin>>n>>m;cout<<a(n,m);return 0;
}