2049: Ackermann's Function
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 3s | 8192K | 928 | 273 | Standard |
In this easy problem, you are to write a program to calculate the Ackermann's Function A:
A(0, n) = n+1
A(m, 0) = A(m-1, 1)
A(m, n) = A(m-1, A(m, n-1))
Note that the Ackermann's Function is recursive but not primitive recursive which means that it grows very fast.
Input
The input contains several lines, each of which consists of two integers m and n, 0<=m, n<=4. The last line consists of two zeros, which you should not proceed.
Output
For each pair of m and n, print the value of A(m, n) in a single line.
Sample Input
2 3 3 3 0 0
Sample Output
9 61
This problem is used for contest: 10
#include<iostream>
#define N 100000
using namespace std;
int a[5][N]={0};
int main()
{
int m,n;
for(int i=0;i<=4;i++)
for(int j=0;j<=N;j++)
{
if(i==0) a[0][j]=j+1;
else
if(j==0) a[i][0]=a[i-1][1];
else
a[i][j]=a[i-1][a[i][j-1]];
}
while(scanf("%d%d",&m,&n),m||n)
{
cout<<a[m][n]<<endl;
}
return 0;
}
/*A(0, n) = n+1
A(m, 0) = A(m-1, 1)
A(m, n) = A(m-1, A(m, n-1))*/
这道题用递归是写不了的 只能用循环了
本文介绍了一个使用循环而非递归的方法来实现Ackermann函数的计算。该函数的特点是非原始递归,增长速度非常快。通过预计算的方式填充二维数组,使得在输入限定范围内能够快速输出Ackermann函数的结果。
1万+

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



