透過輾轉相除法,做出互除得餘數的動作
#include<iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b)
{
while (a != 0 and b != 0) //彼此互除,直到其中一方為0
{
if (a >= b)
{
a = a % b;
}
else if (b > a)
{
b = b % a;
}
}
//要輸出答案時,輸出非0的那一項
if (a >= b)
{
cout << a << endl;
}
else
{
cout << b << endl;
}
}
return 0;
}