阅读:2157回复:0
求最大公约数
{
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. |
|
|