您的位置:首页 > 其它

stein算法求最大公约数

2013-11-04 21:44 190 查看
var
  n,a,b,i:longint;
function gcd(a,b:longint):longint;
begin
  if a=0 then exit(b);
  if b=0 then exit(a);
  if (a and 1=0) and (b and 1=0) then
exit(gcd(a shr 1,b shr 1) shl 1);
  if (a and 1=0) then exit(gcd(a shr
1,b));
  if (b and 1=0) then exit(gcd(a,b shr
1));
  if (a and 1=1) and (b and 1=1) then
exit(gcd(abs(a-b) shr
1,a));{一定要加abs,否则会很惨,(-1)shr 1 是一个很大的数}
end;
begin
  read(n);
  for i:=1 to n do
    begin
     
read(a,b);
     
writeln(gcd(a,b));
    end;
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: