有谁能用递归函数编写一个delphi程序的详细例子给本人看.本人十分感激!!!(附原码,最好能解释,谢谢!!!)

解决方案 »

  1.   

    function nI(n: Integer): Integer; // 计算n的阶乘
    begin
      if n > 0 then // 若n大于1则结果为n 与 (n-1)的阶乘之积
        Result := Result * nI(n - 1)
      else
        Result := 1;// n为0时已到阶乘下限,直接返回1
    end;
      

  2.   

    用递归算阶乘:function a(b:integer):integer;
    begin
      if b=1 then
        result:=1  //1 的结果为1,为结果条件
      else
        result:=b*a(b-1)   //B的结果为B乘(B-1)的阶乘
    end;
      

  3.   

    刚才有些小问题,当B<1时将错误,改为:function a(b:integer):integer;
    begin
      if b<=1 then
        result:=1  //1 的结果为1,为结果条件
      else
        result:=b*a(b-1)   //B的结果为B乘(B-1)的阶乘
    end;递归一定要有结束条件,否则系统堆栈会溢出。
      

  4.   

    // 计算全排列, 删除相同的排列
    procedure StringRank(A, B: String);
    var
       C: Char;
       S: String;
       i, Len: Integer;
    begin
       Len := Length(A);
       if Len = 0 then
          StringPrint(B)
       else for i := 1 to Len do
       begin
          C := A[i];
          S := Copy(A, 1, i - 1);
          if Pos(C, S) = 0 then
             StringRank(S + Copy(A, i + 1, Len - i), C + B);
       end;
    end;例如: 1231 的排列结果有 12 种排列, 结果为, 1321, 3121, 1231, 2131, 3211, 2311, 1312, 3112, 1132, 1213, 2113, 1123
      

  5.   

    我有能力帮助你,请看我站http://www.to-happy.com《大事》里的诸多部分。
    请您告诉我您想实现什么?
      

  6.   

    function cheng(N:Cardianal):Cardinal;
    begin
      Result:=0;
      if N>10000 then 
        showmessage('老大,太大了吧?')
      else
      begin
        if N=0 then 
          Result:=1
        else
          Result:=N*chen(N-1);
      end;
    end;写了一个阶乘的例子,请少爷看看,这个玩意,是12年以前,我在AppleII上面用Basic做的作业。看样子你不太做作业哦!