C++ 如何计算Ackerman';函数调用自身? #包括 使用名称空间std; 长整数A(整数,整数); int main() { int m,n; coutm>>n; cout(0&&n>0) { 内部温度=A(m,n-1); 返回A(m-1,温度); } }

C++ 如何计算Ackerman';函数调用自身? #包括 使用名称空间std; 长整数A(整数,整数); int main() { int m,n; coutm>>n; cout(0&&n>0) { 内部温度=A(m,n-1); 返回A(m-1,温度); } },c++,C++,下面是Ackerman函数的简单代码。我想知道,如果m是常数,这个Ackerman函数将自己作为n的函数调用多少次?我的大脑在试图找出答案时爆炸了。你可以使用count全局变量来找出答案 #include <iostream> using namespace std; long int A(int, int); int main() { int m, n; cout << "Enter two numbers for Ackerman's F

下面是Ackerman函数的简单代码。我想知道,如果m是常数,这个Ackerman函数将自己作为n的函数调用多少次?我的大脑在试图找出答案时爆炸了。

你可以使用count全局变量来找出答案

#include <iostream>

using namespace std;

long int A(int, int);

int main()
{
    int m, n;
        cout << "Enter two numbers for Ackerman's Function." << endl;
        cin >> m >> n;

        cout << A(m, n) << endl;
}

long int A(int m, int n)
{
    if(m == 0)
    {
        return n+1;
    }
    else if(m > 0 && n == 0)
    {
        return A(m-1,1);
    }
    else if(m > 0 && n > 0)
    {
        int temp = A(m,n-1);
        return A(m-1, temp);
    }
}
#包括
使用名称空间std;
长整数A(整数,整数);
整数计数=0;
int main()
{
int m,n;
coutm>>n;

无法将
int&
类型的额外参数添加到
A
函数中:

#include <iostream>

using namespace std;

long int A(int, int);
int count=0;

int main()
{
    int m, n;
        cout << "Enter two numbers for Ackerman's Function." << endl;
        cin >> m >> n;

        cout << A(m, n) << endl;
        count << " Ackerman's Function runs " << count << " times.";
}

long int A(int m, int n)
{   
    count++;
    if(m == 0)
    {
        return n+1;
    }
    else if(m > 0 && n == 0)
    {
        return A(m-1,1);
    }
    else if(m > 0 && n > 0)
    {
        int temp = A(m,n-1);
        return A(m-1, temp);
    }
}
并更新原型,与递归调用匹配

在您的主要功能中:

long int A(int m, int n, int& count)
{   
    count++;
int count=0;
库特
int count = 0;
cout << A(m, n, count) << endl;
cout << "A calls: " << count << endl;