7.最大公约数 欧几里得算法,也叫辗转相除法

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int mod = 1e9 + 7;
 5 int gcd(int a, int b) {
 6     return b ? gcd(b, a % b) : a;
 7 }
 8 int main() {
 9     int n;
10     cin >> n;
11     while (n--) {
12         int a, b;
13         cin >> a >> b;
14         cout << gcd(a, b) << endl;
15     }
16     return 0;
17 }

 时间复杂度大致O(log n)

posted @ 2020-08-04 15:28  kyk333  阅读(241)  评论(0)    收藏  举报