2049: Ackermann's Function

 2049: Ackermann's Function


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K928273Standard

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))*/

这道题用递归是写不了的  只能用循环了 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值