c++求两个数的最小公倍数程序纠错(一定用while语句)

来源:百度知道 编辑:UC知道 时间:2024/06/04 03:36:15
#include<iostream.h>
int main()
{
int a=0,b=0;
cin>>a>>b;
int c=0;
a<=c;
b<=c;
while (c%a!=0&&c%b!=0)
{ c++; }
cout<<c<<endl;
system("pause");
return 0;
}

提供辗转相除法
#include<iostream.h>

int main()
{
int a,b;
cin>>a>>b;
int c;
a<=c;
b<=c;
while(b!=0){
c=a%b;
a=b;
b=c;
}
cout<<a<<endl;
return 0;
}

#include<iostream>
using namespace std;
int main()
{
int a=0,
b=0;
cin>>a>>b;
int c=a;
if(c<b)
c=b;
while((c%a!=0)||(c%b!=0))
{
c++;
}
cout<<c<<endl;
return 0;
}

修改如下:

//---------------------------------------------------------------------------

#include<iostream.h>
int main()
{
int a=0,b=0;
cin>>a>>b;
int c=a; /*注意这里*/
c=c>b?b:c; /*注意这里,相当于if(c>b) c=b;*/
while (c%a!=0||c%b!=0) /*注意这里*/
{ c++; }
cout<<c<<endl;
system("pause&quo