64,643
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
int main()
{
int n,x,y,z;
for(x=1;x<100;x++)
for(y=1;y<100;y++)
for(z=1;z<100;z++)
for(n=1;n<1000;n++)
if((n == x*7)&&(n == y*8)&&(n == z*9))
{
cout << x<< endl;
cout << y<< endl;
cout << z<< endl;
cout << n<< endl;
}
return 0;
}
//...
int temp = max(x, y);
x = min(x, y);
y = min(temp, z);
z = max(temp, z);
for(n = z; n < INT_MAX; n+=z)
if(n%x==0 || n%y==0){
cout<<n/x<<endl;
cout<<n/y<<endl;
cout<<n/z<<endl;
cout<<n<<endl;
}
解释下,z 取 3个整数中的最大值,其他两个给 x 和 y;以 z 的倍数递增查找,而且可以不必判断 z 的整除性。而求最小公倍数法大概如下#include <iostream>
using namespace std;
int gcd(int a, int b)
{
int temp;
if(a < b)
{
temp = a;
a = b;
b = temp;
}
while(b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
int main(void)
{
int x, y, z, n;
cout<< "input 3 integers: "<<endl;
cin >> x >> y >> z;
n = gcd(x, y*z);
cout<< n/x <<endl;
cout<< n/y <<endl;
cout << n/z <<endl;
return 0;
}
以上代码仅供参考,不具备实际用途。