本帖最後由 吳孟修 於 2019-1-5 14:57 編輯
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int x,y,tmp,z=0;
- cout<<"請輸入第一個正整數:";
- cin>>x;
- cout<<"請輸入第二個正整數:";
- cin>>y;
- cout<<x<<"與"<<y<<"的公因數有:";
- tmp=(x>y)?y:x;
- for(int i=1;i<=tmp;i++)
- {
- if(x%i==0 && y%i==0)
- {
- cout<<i<<" ";
- z++;
- }
- }
- cout<<endl;
- cout<<"總共有"<<z<<"個"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |