返回列表 發帖
本帖最後由 高湘庭 於 2024-7-6 15:41 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5.     int x,y,smaller,tmp;
  6. cout<<"請依序輸入兩數";
  7. cin>>x>>y;
  8. cout<<x<<"和"<<y<<"的最大公因數為:";
  9. smaller=x<y?x:y;
  10. for(int i=smaller;i>=1;i--){
  11. if(x%i==0&&y%i==0)
  12.   tmp=i;
  13. break;//***一找到最大公因數就跳出迴圈(由大->小)

  14. }
  15. cout<<tmp<<endl;

  16. system("pause");
  17. return 0;

  18. }
複製代碼

TOP

返回列表