📜  ackermann 函数 pascal 代码示例

📅  最后修改于: 2022-03-11 14:44:44.906000             🧑  作者: Mango

代码示例1
program ackermann;
uses crt;
var
    m, n,  ack_result : integer;

function ack(x,y : LongInt): LongInt;
var
    w, res : LongInt;
begin
    if x = 0 then
        begin
            y := y+1; 
            res := y;
        end
    else if (x>0) and (y=0) then
        begin
            res := ack(x-1,1)
        end
    else if (x>0) and (y>0) then
        begin
        w := ack(x,y-1);
        res := ack(x-1,w); 
        end;
    ack := res;
end;

begin
    clrscr;
    writeln('===== | Ackermann Function | =====');
    WriteLn('Please type m and n value to calculate ack(m, n)');
    Write('m = ');readln(m);
    Write('n = ');readln(n);
    ack_result := ack(m,n);
    WriteLn('Value of ack(', m,',',n,') is = ', ack_result);
    ReadKey;
end.