The Ackermann function is defined recursively for non-negative integers m and n as follows (this presentation is due to Rózsa Péter):
A(m,n)=n+1 if m=0
=A(m-1,1) if m>0 and n=0
=A(m-1,A(m,n-1)) ifm>0 and n>0
要求:
从标准输入中读入一组m和n,两者以空格分隔,输出A(m,n)的值以及计算A(m,n)的全部计算过程
#include<iostream>
using namespace std;
int Ackermann(int m, int n)
{
if (m == 0)
{
cout<< "Ackermann <"<<m<<","<<n<<">"<<endl;
return n + 1;
}
else if(m > 0 && n == 0)
{
cout<< "Ackermann <"<<m<<","<<n<<">"<<endl;
return Ackermann(m - 1, 1);
}
else if(m>0 && n> 0)
{
cout<< "Ackermann <"<<m<<","<<n<<">"<<endl;
return Ackermann(m - 1, Ackermann(m, n - 1));
}
else
{
return 0;
}
}
int main()
{
int m,n;
cin>>m>>n;
cout<<Ackermann(m,n)<<endl;
return 0;
}
本文介绍了一个使用C++实现的阿克曼函数计算程序。该程序能够递归地计算给定非负整数m和n的阿克曼函数值,并输出整个计算过程。阿克曼函数是一种经典的双参数递归函数,在计算机科学中用于测试和研究递归调用。
1696

被折叠的 条评论
为什么被折叠?



