gpt4 book ai didi

c - C 中两个数的 L.C.M

转载 作者:行者123 更新时间:2023-11-30 16:12:45 27 4
gpt4 key购买 nike

所以,这是我计算 L.C.M 的代码(最小公倍数)不使用G.C.D :

int lcm(int x, int y){

int max = 0, min = 0, ans = 0;

if(y >= x){
max = y;
min = x;
if(y % x == 0) return y;
}else {
max = x;
max = y;
if(x % y == 0) return x;
}

for(int i = 1; i <= max ; i++){
if( (max*i) % min == 0){
ans = max * i;
break;
}
}

return ans;
}

这是主要:

int main(){

int u, v;

printf("Input two numbers: ");
scanf("%d%d", &u, &v);
puts("");
printf("LCM(%d, %d): %d",u , v, lcm(u, v));

return 0;
}

它非常适合 4 87 21 以及其他第一个数字较小的输入。一个例子:

It takes a lot of time to run if the value of first input is higher and does nothing

我在这里做错了什么?

我正在使用 Dev-C++。

最佳答案

lcm函数内的else语句中,它应该是min = y。这就是我犯的错误。谢谢TotallyNoob感谢您指出这一点。

关于c - C 中两个数的 L.C.M,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58263017/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com