18.2.13 codevs1212 最大公約數
阿新 • • 發佈:2018-02-14
-a 常見 urn alt baidu ane color rip dev
題目描述 Description
求兩個數A和B的最大公約數。 1<=A,B<=2^31-1
輸入描述 Input Description兩個整數A和B
輸出描述 Output Description最大公約數gcd(A,B)
樣例輸入 Sample Input8 12
樣例輸出 Sample Output4
1 #include <iostream> 2 #include<math.h> 3 4 using namespace std;View Code5 6 long dig(long x,long y)//y>x 7 { 8 long yy=x,xx=y%x; 9 if(xx==0) 10 return x; 11 else 12 dig(xx,yy); 13 } 14 15 int main() 16 { 17 long x,y; 18 cin>>x>>y; 19 if(x<=y) 20 cout<<dig(x,y); 21 else 22 cout<<dig(y,x);23 return 0; 24 }
求解最大公約數最常見的兩種辦法:輾轉相除法和更相減損法
難道只有我一個人完全不知道這兩種方法是啥玩意?!
輾轉相除法
18.2.13 codevs1212 最大公約數