#16220: 檢測對 上傳後 NA


jinyulin34@gmail.com (jinyulink)

學校 : 國立中央大學
編號 : 82369
來源 : [140.115.130.200]
最後登入時間 :
2024-11-29 14:33:50

#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 ... (少年π)

學校 : 臺北市私立延平高級中學
編號 : 69058
來源 : [223.137.60.223]
最後登入時間 :
2025-05-23 17:43:44

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