两个数的最小公倍数

发布于 2024-10-20 08:00:44 字数 321 浏览 6 评论 0原文

我的 LCM 程序得到错误的结果。

我首先找到数字的最大公约数,然后将乘积除以最大公约数。

int gcd(int x, int y)
{
  while(y != 0)
  {
    int save = y;
    y = x % y;
    x = save;
  }
  return y;
}

int lcm(int x, int y)
{
  int prod = x * y;
  int Gcd = gcd(x,y);
  int lcm = prod / Gcd;

  return lcm;
}

非常感谢任何帮助。

I am getting wrong result for my LCM program.

Ifirst find gcd of the numbers and then divide the product with gcd.

int gcd(int x, int y)
{
  while(y != 0)
  {
    int save = y;
    y = x % y;
    x = save;
  }
  return y;
}

int lcm(int x, int y)
{
  int prod = x * y;
  int Gcd = gcd(x,y);
  int lcm = prod / Gcd;

  return lcm;
}

Any help much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡紫姑娘! 2024-10-27 08:00:44

您的 gcd 函数将始终返回 0。更改

return y;

return x;

理解欧几里得算法:

RULE 1: gcd(x,0) = x
RULE 2: gcd(x,y) = gcd(y,x % y)

考虑 x = 12y = 18

  gcd (12, 18)
  = gcd (18, 12)  Using rule 2
  = gcd (12,6)    Using rule 2
  = gcd (6, 0)    Using rule 1
  = 6

正如您所看到的,当 y 变为零时 x 将是 gcd,因此您需要返回 x 而不是 y

另外,在计算 lcm 时,您首先将数字相乘,这可能会导致溢出。相反,你可以这样做:

lcm = x * (y / gcd(x,y))

但如果 lcm 无法放入 int 中,则必须将其设为 long long

Your gcd function will always return 0. Change

return y;

to

return x;

Understand the Euclid's algorithm:

RULE 1: gcd(x,0) = x
RULE 2: gcd(x,y) = gcd(y,x % y)

consider x = 12 and y = 18

  gcd (12, 18)
  = gcd (18, 12)  Using rule 2
  = gcd (12,6)    Using rule 2
  = gcd (6, 0)    Using rule 1
  = 6

As you can see when y becomes zero x will be the gcd so you need to return x and not y.

Also while calculating lcm you are multiplying the numbers first which can cause overflow. Instead you can do:

lcm = x * (y / gcd(x,y))

but if lcm cannot fit in an int you'll have to make it long long

懒猫 2024-10-27 08:00:44

问题1) int gcd = gcd(x,y);

gcd 已经被定义为一个函数。不能定义同名的变量。

问题2)将gcd()中的return y改为return x,否则每次都会返回0。

问题3)如果xy很大,x * y可能会溢出。

Problem 1) int gcd = gcd(x,y);

gcd is already defined to be a function. You cannot define a variable with the same name.

Problem 2) Change return y to return x in gcd() otherwise 0 will be returned everytime.

Problem 3) x * y may overflow if x and y are large.

归途 2024-10-27 08:00:44

您应该在 gcd 函数中返回 x 而不是 y。

另外,您确定产品 x*y 始终适合 int 吗?使用 long long 也可能是个好主意。

You should return x instead of y in your gcd function.

Also, are you sure the product x*y will always fit into an int? Might be a good idea to use a long long for that as well.

世界等同你 2024-10-27 08:00:44
#include <iostream>

using namespace std; 

long long gcd(long long int a, long long int b){
    if(b==0)
        return a;
    return gcd(b,a%b);
}

long long lcm(long long a,long long b){
    if(a>b)
        return (a/gcd(a,b))*b;
    else
        return (b/gcd(a,b))*a;
} 

int main(){
    long long int a ,b ;
    cin>>a>>b;
    cout<<lcm(a,b)<<endl;
    return 0;
}
#include <iostream>

using namespace std; 

long long gcd(long long int a, long long int b){
    if(b==0)
        return a;
    return gcd(b,a%b);
}

long long lcm(long long a,long long b){
    if(a>b)
        return (a/gcd(a,b))*b;
    else
        return (b/gcd(a,b))*a;
} 

int main(){
    long long int a ,b ;
    cin>>a>>b;
    cout<<lcm(a,b)<<endl;
    return 0;
}
旧时光的容颜 2024-10-27 08:00:44

这个 C 程序是寻找 LCM 的不同方法

 #include<stdio.h>
    int main()
    {
        int a,b,lcm=1,i=2;
        printf("Enter two numbers to find LCM\n" );
        scanf("%d %d",&a ,&b);
        while(i <= a*b)
        {
            if(a%i==0 & b%i==0)
            {
                lcm=lcm*i;
                a=a/i;
                b=b/i;
                i=i-1;
            }
            if( a%i==0 & b%i!=0)
            {
                lcm=lcm*i;
                a=a/i;
                i=i-1;
            }
            if( b%i==0 & a%i!=0)
            {
                lcm=lcm*i;
                b=b/i;
                i=i-1;
            }
            i++;
        }
        printf("The LCM of numbers is %d\n", lcm);
    }

This C program is different approach towards finding LCM

 #include<stdio.h>
    int main()
    {
        int a,b,lcm=1,i=2;
        printf("Enter two numbers to find LCM\n" );
        scanf("%d %d",&a ,&b);
        while(i <= a*b)
        {
            if(a%i==0 & b%i==0)
            {
                lcm=lcm*i;
                a=a/i;
                b=b/i;
                i=i-1;
            }
            if( a%i==0 & b%i!=0)
            {
                lcm=lcm*i;
                a=a/i;
                i=i-1;
            }
            if( b%i==0 & a%i!=0)
            {
                lcm=lcm*i;
                b=b/i;
                i=i-1;
            }
            i++;
        }
        printf("The LCM of numbers is %d\n", lcm);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文