給兩個數 a, b
計算 a, b 的最大公因數
評分結果(參考) : AC (2ms, 332KB)
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a, b;
while(cin >> a >> b){
cout << __gcd(a, b) << "\n";
}
return 0;
}
以 gcd(49, 28) 舉例
計算步驟如下
評分結果(參考) : AC (2ms, 344KB)
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a, b;
while(cin >> a >> b){
while(a%b != 0){
a = a%b;
swap(a, b);
}
cout << b << "\n";
}
return 0;
}