Ackerman
递归算法
一 . 问题描述及分析
图1
二 . 代码实现
package other;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class bin_1
{
public static void main(String[] args) throws IOException
{
Ackerman myAckerman=new Ackerman(10, 2);
}
}
class Ackerman
{
int m;
int n;
int result;
public Ackerman(int n,int m) throws IOException
{
this.n=n;
this.m=m;
result=ackerman(n,m);
display();
}
public int ackerman(int n,int m)
{
if(n==1&&m==0)
{
return 2;
}
if(n==0&&m>=0)
{
return 1;
}
if(n>=2&&m==0)
{
return n+2;
}
return ackerman(ackerman(n-1,m),m-1);
}
public void display() throws IOException
{
BufferedWriter fout=new BufferedWriter(new FileWriter("out.txt"));
fout.write("result="+result);
fout.flush();
}
}
三 . 运行结果
Ackerman myAckerman=new Ackerman(10, 2);
enter description here