问题描述
阿克曼(Ackmann)函数 A(m,n)A(m,n) 中, m,nm,n 的取值范围是 (m≤3,n≤10)(m≤3,n≤10),函数定义为:
输入格式
输入两个整数 m,n(0≤m≤3,0≤n≤10)m,n(0≤m≤3,0≤n≤10) 。
输出格式
输出 A(m,n)A(m,n) 值。
样例输入
2 3
样例输出
9
这道题是很基本的递推,只要参照右边的判断关系式写出return出左边的代码就行了;
先写出输入输出和函数:
#include<bits/stdc++.h>
using namespace std;
int main(){
int q,w;
cin>>q>>w;
cout<<f(q,w);
return 0;
}
再定义函数:
#include<bits/stdc++.h>
using namespace std;
int f(int m,int n){
}
int main(){
int q,w;
cin>>q>>w;
cout<<f(q,w);
return 0;
}
参照上面的导图写代码:
#include<bits/stdc++.h>
using namespace std;
int f(int m,int n){
if(m==0) return n+1;
if(m>0&&n==0) return f(m-1,1);
return f(m-1,f(m,n-1));
}
int main(){
int q,w;
cin>>q>>w;
cout<<f(q,w);
return 0;
}