#16220: 檢測對 上傳後 NA


jinyulin34@gmail.com (jinyulink)


#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;
}

#16221: Re:檢測對 上傳後 NA


314159265358979323846264338327 ... (少年π)


#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()來完成(程式碼會比較精簡)