標題:
有哪些因數 (九) - 求最大公因數 (輾轉相除法)
[打印本頁]
作者:
鄭繼威
時間:
2023-3-17 01:33
標題:
有哪些因數 (九) - 求最大公因數 (輾轉相除法)
試以輾轉相除法,解最大公因數。
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
int x,y,tmp;
cout<<"請依序輸入兩個正整數: ";
cin>>x>>y;
cout<<x<<"與"<<y<<"的最大公因數為: ";
//看到0才代表輾轉相除法結束
while(x%y!=0)
{
//一直交換
tmp=x%y;
x=y;
y=tmp;
}
cout<<y<<endl<<endl;
goto re;
system("pause");
return 0;
}
/*
x y
35 / 56 = 0 ... 35
56 / 35 = 1 ... 21
35 / 21 = 1 ... 14
21 / 14 = 1... 7
14 / 7 = 2 ... 0
*/
複製代碼
作者:
蔡沛倢
時間:
2023-3-17 20:10
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int a,b,c=0,d=0,f=0;
cout<<"請輸入兩個整數:";
cin>>a>>b;
cout<<a<<"和"<<b<<"的最大公因數為:";
while(a%b!=0){
f=a%b;
a=b;
b=f;
}
cout<<b<<endl;
system("pause");
return 0;
}
複製代碼
作者:
黃子豪
時間:
2023-3-17 20:10
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x,y,tmp;
cout<<"輸入第一正整數: ";
cin>>x;
cout<<"輸入第二正整數: ";
cin>>y;
while(x%y!=0){
tmp=x%y;
x=y;
y=tmp;
}
cout<<"最大公因數:"<<y<<endl;
system("pause");
return 0;
}
複製代碼
作者:
呂得銓
時間:
2023-3-17 20:10
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
int x,y,tmp;
cout<<"請依序輸入兩個正整數: ";
cin>>x>>y;;
cout<<x<<"與"<<y<<"的最大公因數為: ";
while(x%y!=0){
tmp=x%y;
x=y;
y=tmp;}
cout<<y<<endl<<endl;
system("pause");
goto re;
return 0;
}
複製代碼
作者:
廖秝瑜
時間:
2023-3-17 20:11
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x,y,tmp;
cout<<"請依序輸入兩個正整數:";
cin>>x>>y;
cout<<x<<"與"<<y<<"的最大公因數為:";
while(x%y!=0)
{
tmp=x%y;
x=y
y=tmp
}
cout<<y<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
邵凡榛
時間:
2023-3-17 20:11
本帖最後由 邵凡榛 於 2023-3-17 20:19 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int x,y,tmp;
cout<<"請輸入第1個正整數:";
cin>>x;
cout<<"請輸入第2個正整數:";
cin>>y;
cout<<x<<"與"<<y<<"的最大公因數為:";
while(x%y!=0)
{
tmp=x%y;
x=y;
y=tmp;
}
cout<<y<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
何權晉
時間:
2023-3-17 20:15
#include<iostream>
using namespace std;
int main()
{
int x,y, tmp;
cout<<"Enter two random whole numbers: ";
cin>>x>>y;
cout<<x<<"&"<<y<<"'s greatest common factor: ";
while(x%y!=0)
{
tmp=x%y;
x=y;
y=tmp;
}
cout<<y<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
曹祁望
時間:
2023-3-17 20:20
#include<iostream>
#include<cstdlib>
using namespace std;
class Demo{
public:
int a, b;
Demo(int a, int b){
this->a = a;
this->b = b;
}
int DoSomething(){
int tmp;
while(this->a%this->b!=0){
tmp=this->a%this->b;
this->a=this->b;
this->b=tmp;
}
return this->b;
}
};
int main(){
int x, y;
cout<<"請輸入兩數:";
cin>>x>>y;
Demo hi(x,y);
cout<<"最大公因數為:"<<hi.DoSomething()<<endl;
system("pause");
return 0;
}
複製代碼
作者:
鄭繼威
時間:
2023-3-17 20:21
7
作者:
盧玄皓
時間:
2023-3-17 20:21
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
int x,y,tmp;
cout<<"請依序輸入兩個正整數: ";
cin>>x>>y;
cout<<"與"<<y<<"的最大公因數為: ";
while(x%y!=0)
{
tmp=x%y;
x=y;
y=tmp;
}
cout<<y<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
作者:
張絜晰
時間:
2023-3-17 20:30
本帖最後由 張絜晰 於 2023-3-17 21:02 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int a,b,f=0;
cout<<"請輸入兩個整數:";
cin>>a>>b;
while(a%b!=0){
f=a%b;
a=b;
b=f;
}
cout<<a<<"和"<<b<<"的最大公因數是"<<b<<endl;
system("pause");
return 0;
}
複製代碼
作者:
呂宗晉
時間:
2023-3-19 14:27
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
int x,y,tmp;
cout<<"請依序輸入兩個正整數: ";
cin>>x>>y;;
cout<<x<<"與"<<y<<"的最大公因數為: ";
while(x%y!=0){
tmp=x%y;
x=y;
y=tmp;}
cout<<y<<endl<<endl;
system("pause");
goto re;
return 0;
}
複製代碼
作者:
邱品惟
時間:
2023-6-26 16:40
#include<iostream>
using namespace std;
int main()
{
int x,y,tmp;
cout<<"請依序輸入兩個正整數: ";
cin>>x>>y;
cout<<x<<"與"<<y<<"的最大公因數為: ";
while(x%y!=0)
{
tmp=x%y;
x=y;
y=tmp;
}
cout<<y<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2