C++中两个数的最小公倍数(LCM)

最小公倍数(LCM)用于获取两个数(n1和n2)的最小公倍数,并且最小公倍数应该能被给定的两个数整除。公倍数是两个数中共有的数。两个数的最小公倍数可以表示为LCM(a,b)或lcm(a,b)。

1.png

例如,两个正数的最小公倍数,如LCM(12,24)为24。因为12和24都能被24整除,没有余数。同样,3和4的最小公倍数是12,因为两个数的最小公倍数是12。

两个数的最小公倍数的算法

步骤1:从用户那里获取两个输入n1和n2。

步骤2:将n1和n2的最小公倍数存储到max变量中。

步骤3:验证max变量是否能被n1和n2整除,将max作为两个数的LCM打印出来。

步骤4:否则,每次迭代时将max值更新1次,并跳转到步骤3以检查max变量的可被整除性。

步骤5:终止程序。

使用if语句和while循环来获取两个数的LCM的程序

Program1.cpp

#include <iostream>
using namespace std;
int main()
{
    int n1, n2, max_num, flag = 1;
    cout << " Enter two numbers: \n";
    cin >> n1 >> n2;

    // 使用三元运算符获取较大的数
    max_num = (n1 > n2) ? n1 : n2;

    while (flag)
    {
        // 使用if语句检查max_num是否被n1和n2完全整除。
        if(max_num % n1 == 0 && max_num % n2 == 0)
        {
            cout << " The LCM of " << n1 << " and " << n2 << " is " << max_num;
            break;
        }
        ++max_num; // 每次迭代更新1次
    }
    return 0;
}

输出

Enter two numbers:
30
50
 The LCM of 30 and 50 is 150

使用while循环来获取两个数的LCM的程序

Program2.cpp

#include <iostream>
using namespace std;

int main()
{
    int num1, num2, lcm, gcd, temp;
    cout << " Enter the first number: ";
    cin >> num1;
    cout << " Enter the second number: ";
    cin >> num2;

    int a = num1;
    int b = num2;
    while (num2 != 0)
    {
        temp = num2;
        num2 = num1 % num2;
        num1 = temp;
    }

    gcd = num1;
    lcm = (a * b) / gcd;
    cout << "\n LCM of " << a << " and " << b << " = " << lcm;
    return 0;
}

输出

Enter the first number: 15
 Enter the second number: 10

 LCM of 15 and 10 = 30

使用GCD来获取两个数的LCM的程序

Program3.cpp

#include <iostream>
using namespace std;

int getGCD(int n1, int n2)
{
    if (n1 == 0 || n2 == 0)
        return 0;

    if (n1 == n2)
        return n1;

    if (n1 > n2)
        return getGCD(n1 - n2, n2);

    return getGCD(n1, n2 - n1);
}

int getLCM(int n1, int n2)
{
    return (n1 * n2) / getGCD(n1, n2);
}

int main()
{
    int n1, n2;
    cout << " Enter two positive numbers: ";
    cin >> n1 >> n2;

    cout << " \n LCM of " << n1 << " and " << n2 << " is " << getLCM(n1, n2);
    return 0;
}

输出

Enter two positive numbers: 50
60

 LCM of 50 and 60 is 300

使用递归函数来获取两个数的LCM的程序

Program 4.cpp

#include <iostream>
using namespace std;

int getGCD(int n1, int n2)
{
    if (n1 == 0)
    {
        return n2;
    }

    return getGCD(n2 % n1, n1);
}

int getLCM(int n1, int n2)
{
    return (n1 / getGCD(n1, n2)) * n2;
}

int main()
{
    int num1, num2;
    cout << " Enter two numbers: " << endl;
    cin >> num1 >> num2;
    cout << " LCM of two numbers " << num1 << " and " << num2 << " is " << getLCM(num1, num2);
}

输出

Enter two numbers:
12
36
 LCM of two numbers 12 and 36 is 36

使用函数和while循环来获取多个数组元素的LCM的程序 Program5.cpp

#include <iostream>
using namespace std;

int lcm_num(int n1, int n2)
{
    int max;
    max = (n1 > n2) ? n1 : n2;
    while (true)
    {
        if (max % n1 == 0 && max % n2 == 0)
            return max;
        max++;
    }
}

int lcm_array(int arr[], int num)
{
    int i;
    int lcm = lcm_num(arr[0], arr[1]);

    for (i = 2; i < num; i++)
    {
        lcm = lcm_num(lcm, arr[i]);
    }
    return lcm;
}

int main()
{
    int arr[] = {10, 5, 15, 30};
    int num = sizeof(arr) / sizeof(arr[0]);
    cout << " LCM of multiple array elements is: " << lcm_array(arr, num);
}

输出

LCM of multiple array elements is: 30

标签: C++语言, C++语言教程, C++语言技术, C++语言学习, C++语言学习教程, C++语言下载, C++语言开发, C++语言入门教程, C++语言进阶教程, C++语言高级教程, C++语言面试题, C++语言笔试题, C++语言编程思想