返回列表 發帖

有哪些因數 (九) - 求最大公因數 (輾轉相除法)



試以輾轉相除法,解最大公因數。
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.     re:
  7.     int x,y,tmp;
  8.     cout<<"請依序輸入兩個正整數: ";
  9.     cin>>x>>y;
  10.     cout<<x<<"與"<<y<<"的最大公因數為: ";

  11.     //看到0才代表輾轉相除法結束
  12.     while(x%y!=0)
  13.     {
  14.         //一直交換
  15.         tmp=x%y;
  16.         x=y;
  17.         y=tmp;   
  18.     }
  19.     cout<<y<<endl<<endl;
  20.     goto re;
  21.     system("pause");
  22.     return 0;   
  23. }
  24. /*
  25.     x   y
  26.     35 / 56 = 0 ... 35
  27.     56 / 35 = 1 ... 21
  28.     35 / 21 = 1 ... 14
  29.     21 / 14 = 1... 7
  30.     14 / 7 = 2 ... 0
  31. */
複製代碼

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {  
  5.     int x,y,tmp;
  6.     cout<<"請依序輸入兩個正整數: ";
  7.     cin>>x>>y;
  8.     cout<<x<<"與"<<y<<"的最大公因數為: ";
  9.     while(x%y!=0)
  10.     {
  11.         tmp=x%y;
  12.         x=y;
  13.         y=tmp;   
  14.     }
  15.     cout<<y<<endl;
  16.     system("pause");
  17.     return 0;   
  18. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.           int x,y,tmp;
  8.           cout<<"請依序輸入兩個正整數: ";
  9.           cin>>x>>y;;
  10.           cout<<x<<"與"<<y<<"的最大公因數為: ";
  11.           while(x%y!=0){
  12.           tmp=x%y;
  13.           x=y;
  14.           y=tmp;}
  15.           cout<<y<<endl<<endl;
  16.           system("pause");
  17.           goto re;
  18.           return 0;
  19. }
複製代碼

TOP

本帖最後由 張絜晰 於 2023-3-17 21:02 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.    int a,b,f=0;
  7.    cout<<"請輸入兩個整數:";
  8.    cin>>a>>b;
  9.    while(a%b!=0){
  10.    f=a%b;
  11.    a=b;
  12.    b=f;
  13. }
  14.    cout<<a<<"和"<<b<<"的最大公因數是"<<b<<endl;
  15.    system("pause");
  16.    return 0;   
  17. }
複製代碼
Attention Seeker </3

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      re:
  7.     int x,y,tmp;
  8.     cout<<"請依序輸入兩個正整數: ";
  9.     cin>>x>>y;
  10.     cout<<"與"<<y<<"的最大公因數為: ";
  11.     while(x%y!=0)
  12.     {
  13.     tmp=x%y;
  14.     x=y;
  15.     y=tmp;
  16.     }
  17.     cout<<y<<endl;
  18.     goto re;
  19.     system("pause");
  20.     return 0;
  21. }
複製代碼

TOP

7

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. class Demo{
  5.       public:
  6.              int a, b;
  7.              Demo(int a, int b){
  8.                       this->a = a;
  9.                       this->b = b;         
  10.              }
  11.              int DoSomething(){
  12.                  int tmp;
  13.                  while(this->a%this->b!=0){
  14.                      tmp=this->a%this->b;
  15.                      this->a=this->b;
  16.                      this->b=tmp;
  17.                  }
  18.                  return this->b;
  19.              }              
  20.       
  21.       
  22. };
  23. int main(){
  24.     int x, y;
  25.     cout<<"請輸入兩數:";
  26.     cin>>x>>y;
  27.     Demo hi(x,y);
  28.     cout<<"最大公因數為:"<<hi.DoSomething()<<endl;
  29.     system("pause");
  30.     return 0;   
  31. }
複製代碼

TOP

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int x,y, tmp;
  6.     cout<<"Enter two random whole numbers: ";
  7.     cin>>x>>y;
  8.    
  9.     cout<<x<<"&"<<y<<"'s greatest common factor: ";
  10.     while(x%y!=0)
  11.     {
  12.      tmp=x%y;
  13.      x=y;
  14.      y=tmp;
  15.      }
  16.      cout<<y<<endl<<endl;
  17.                       system("pause");
  18.                       return 0;
  19. }
複製代碼

TOP

本帖最後由 邵凡榛 於 2023-3-17 20:19 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.     int x,y,tmp;
  7.     cout<<"請輸入第1個正整數:";
  8.     cin>>x;
  9.     cout<<"請輸入第2個正整數:";
  10.     cin>>y;
  11.     cout<<x<<"與"<<y<<"的最大公因數為:";
  12.    
  13.     while(x%y!=0)
  14.     {
  15.         tmp=x%y;
  16.         x=y;
  17.         y=tmp;
  18.     }
  19.      cout<<y<<endl<<endl;

  20.     system("pause");
  21.     return 0;   
  22. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,tmp;
  7.     cout<<"請依序輸入兩個正整數:";
  8.     cin>>x>>y;
  9.     cout<<x<<"與"<<y<<"的最大公因數為:";
  10.    
  11.     while(x%y!=0)
  12.     {
  13.                  tmp=x%y;
  14.                  x=y
  15.                  y=tmp
  16.                  }
  17.                  cout<<y<<endl<<endl;
  18.    
  19.     system("pause");
  20.     return 0;
  21. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.           int x,y,tmp;
  8.           cout<<"請依序輸入兩個正整數: ";
  9.           cin>>x>>y;;
  10.           cout<<x<<"與"<<y<<"的最大公因數為: ";
  11.           while(x%y!=0){
  12.           tmp=x%y;
  13.           x=y;
  14.           y=tmp;}
  15.           cout<<y<<endl<<endl;
  16.           system("pause");
  17.           goto re;
  18.           return 0;
  19. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x,y,tmp;
  7.     cout<<"輸入第一正整數: ";
  8.     cin>>x;
  9.     cout<<"輸入第二正整數: ";
  10.     cin>>y;
  11.    
  12.     while(x%y!=0){
  13.         tmp=x%y;
  14.         x=y;
  15.         y=tmp;
  16.     }
  17.     cout<<"最大公因數:"<<y<<endl;
  18.    
  19.     system("pause");
  20.     return 0;
  21. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.    int a,b,c=0,d=0,f=0;
  7.    cout<<"請輸入兩個整數:";
  8.    cin>>a>>b;
  9.    cout<<a<<"和"<<b<<"的最大公因數為:";
  10.    while(a%b!=0){
  11.    f=a%b;
  12.    a=b;
  13.    b=f;        
  14.                  }
  15.    cout<<b<<endl;
  16.    system("pause");
  17.    return 0;   
  18. }
複製代碼

TOP

返回列表