题目:
当一个函数及它的变量是由函数自身定义时,称这个函数是双递归函数。双递归函数Ackerman函数定义如下:
计算A(3,2)。
根据公式很容易写出代码,直接附上:
// Chapter7_6.cpp : Defines the entry point for the application.
// 双递归函数Ackerman,计算A(3,2).
#include "stdafx.h"
#include<iostream>
using namespace std;
int Ackerman(int n,int m)
{
if(n ==1 && m == 0)
return 2;
else if(n == 0)
return 1;
else if(m == 0)
return n+2;
else
Ackerman(Ackerman(n-1,m),m-1);
}
int main()
{
int result;
result = Ackerman(3,2);
cout << "the answer is: " << result << endl;
system("pause");
return 0;
}
运行结果如下: