問題描述:
給定二個正整數,利用輾轉相除法求其最大公因數。
輸入說明:
給定二個正整數
輸出說明:
輸出最大公因數
範例:
假設輸入為 300 與 250, 則輸出為 50
C++:
//假設輸入為 300 與 250, 則輸出為 50
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
//ios::sync_with_stdio(0);
//cin.tie(0);
int a,b;
while(cin>>a>>b){
cout<<__gcd(a,b)<<"\n";
}
return 0;
}
python:
import math
while True:
try:
a,b=map(int,input().split())
print(math.gcd(a,b))
except:break
文章標籤
全站熱搜
