330
技術社區[雲棲]
C++求最大公約數與最小公倍數
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
int p,r,n,m,temp;
cout<<"please enter two positive integer numbers n,m:";
cin>>n>>m;
//n存放最小數,m存放最大數
if(n>m){
temp = n;
n = m;
m = n;
}
p=n*m;//先取得兩個數的積
while(n!=0){
r=m%n;
m=n;
n=r;
}
cout<<"最大公約數:"<<m<<endl;
cout<<"最小公倍數:"<<p/m<<endl;
}
最後更新:2017-04-02 22:16:39