[Codeforces Testing Round #5]A. Subtractions

地址:http://codeforces.com/contest/267/problem/A

更相减损,也是求最大公约数的方法,本题求用了多少次减法

用辗转相除也是可以的,相当于把每一步求余时的商相加

担心很大的数相除会出问题,所以用long long

 1 #include<stdio.h>
 2 
 3 int n;
 4 long long a,b;
 5 
 6 int main()
 7 {
 8     long long i,t,ans;
 9     scanf("%d",&n);
10     for(i=0;i<n;i++)
11     {
12         ans=0;
13         scanf("%lld %lld",&a,&b);
14         while(a!=0 && b!=0)
15         {
16             ans=ans+a/b;
17             t=a;
18             a=b;
19             b=t%b;
20         }
21         printf("%lld\n",ans);
22     }
23     return 0;
24 }

 

posted @ 2013-01-18 20:58  tjsuhst  阅读(230)  评论(0编辑  收藏  举报