#include<iostream> using namespace std; int gcd(int n1, int n2) { while (n2 != 0) { int temp = n1; n1= n2; n2 = temp % n2; } return n1; } int main() { int a, b, c; cin >> a >> b >> c; int gcd(int, int, int); int s = gcd(a, b, c); int t = a * b / s && int t = (b * c / s )&& int t = (a * c / s); cout << "gcd(" << a << "," << b << "," << c << ")=" << s << endl; cout << "lcm(" << a << "," << b << "," << c << ")=" << t << endl; }
时间: 2023-06-14 14:05:59 浏览: 107
这段代码有一些问题:
1. 在 main 函数中定义了一个 int gcd(int, int, int) 的函数指针,但是并没有使用它,而是直接调用了全局定义的 gcd 函数。这个函数指针定义应该删除。
2. 在计算 lcm(最小公倍数)的时候,使用了 && 连接了三个表达式,但是这是错误的,应该使用 * 连接。正确的表达式应该是:int t = (a * b / s) * (b * c / s) * (a * c / s);
下面是修改后的代码:
```
#include<iostream>
using namespace std;
int gcd(int n1, int n2) {
while (n2 != 0) {
int temp = n1;
n1= n2;
n2 = temp % n2;
}
return n1;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
int s = gcd(gcd(a, b), c);
int t = (a * b / s) * (b * c / s) * (a * c / s);
cout << "gcd(" << a << "," << b << "," << c << ")=" << s << endl;
cout << "lcm(" << a << "," << b << "," << c << ")=" << t << endl;
}
```
阅读全文