gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15945
  • QQ554730525
  • 铜币25337枚
  • 威望15352点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
阅读:2029回复:0

求最大公约数

楼主#
更多 发布于:2003-08-06 23:09
{
  Author    : tenshi
  Date      : 2002-03-12
  Problem   : Basic Math -- Greatest Common Divisor
  Algorithm : Zhan Zhuan Xiang Chu Fa
  Input     : two lines, each line a positive integer
  Output    : GCD of them
}
program BasicMath_GCD;
const
  fn_inp='bmath1.inp';
  fn_out='bmath1.out';
var
  x,y:longint; { two input integers }
  ans:longint; { GCD of them }


  procedure init;
  begin
    assign(input,fn_inp);
    reset(input);
    readln(x,y);
    close(input);
  end;

  function gcd(x,y:longint):longint;
  begin
    if y=0 then gcd:=x
           else gcd:=gcd(y,x mod y);
  end;

  procedure main;
  begin
    ans:=gcd(x,y);
    {
      //here is a simple method :
      while(y>0) do
      begin
        tmp:=y;
        y:=x mod y;
        x:=tmp;
      end;
      ans:=x;
    }
  end;

  procedure out;
  begin
    assign(output,fn_out);
    rewrite(output);
    writeln(ans);
    close(output);
  end;

  begin
    init;
    main;
    out;
  end.
喜欢0 评分0
游客

返回顶部