#include <iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
if(a>b)
{
int x;
x=a%b;
if(x==0)
{
cout<<b<<"\n";
}
else
{
cout<<x<<"\n";
}
}
else if(b>a)
{
int x;
x=b%a;
if(x==0)
{
cout<<a<<"\n";
}
else
{
cout<<x<<"\n";
}
}
else if(a==b)
{
cout<<a<<endl;
}
}
return 0;
}
#include
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
if(a>b)
{
int x;
x=a%b;
if(x==0)
{
cout<<b<<"\n";
}
else
{
cout<<x<<"\n";
}
}
else if(b>a)
{
int x;
x=b%a;
if(x==0)
{
cout<<a<<"\n";
}
else
{
cout<<x<<"\n";
}
}
else if(a==b)
{
cout<<a<<endl;
}
}
return 0;
}
粗體部分不對(輾轉相除不一定只除一次,請用while迴圈)
這題建議用algorithm標頭檔裡的__gcd()來完成(程式碼會比較精簡)