Problem Description

給定兩個數字,請得出它們的最大公因數   

The Input

兩個整數 大於 0, 小於 231

The Output

最大公因數為一整數

Sample Input

12     15

Sample Output

3

-----* Problem from【ZeroJudge, an Online Judge System For Beginners

My Answer

 
1
2
3
4
5
6
7
8
9
10
11
12
13
  #include<iostream>
using namespace std;
int main(void)
{
  int a, b;
  while(cin>>a>>b){
    do{
      (a>b) ? a=a%b : b=b%a;
    }while(a!=0 && b!=0);
  (a==0) ? cout<<b<<endl : cout<<a<<endl;
  }
  return 0;
}
 

 


arrow
arrow
    全站熱搜

    兔老大 發表在 痞客邦 留言(0) 人氣()