close

問題描述:

給定二個正整數,利用輾轉相除法求其最大公因數。

輸入說明:

給定二個正整數

輸出說明:

輸出最大公因數

範例:

假設輸入為 300 與 250, 則輸出為 50

C++

#include <iostream>
using namespace std;

int main() {
    int a,b;
    while (cin>>a>>b)
    {
        while (b!=0)
        {
            int temp=b;
            b=a%b;
            a=temp;
        }
        cout<<a<<"\n";
    }
    return 0;
}

python

while True:
    try:
        n,m=map(int,input().split())
        while m!=0:
            temp=m
            m=n%m
            n=temp
        print(n)
    except:break

arrow
arrow
    文章標籤
    python C++ ITSA
    全站熱搜
    創作者介紹
    創作者 趴趴熊日常 的頭像
    趴趴熊日常

    資工趴趴熊的小天地

    趴趴熊日常 發表在 痞客邦 留言(0) 人氣()