计算 GCD 和 LCM

C++C++Beginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

引言

在数学中,GCD(最大公约数,Greatest Common Divisor)和 LCM(最小公倍数,Least Common Multiple)是非常有用的概念。最大公约数,也称为最大公因数,是指两个数中最大的能够同时整除这两个数的数。相反,最小公倍数是指两个或多个整数中最小的能够被所有这些整数整除的正整数。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/IOandFileHandlingGroup -.-> cpp/files("`Files`") cpp/SyntaxandStyleGroup -.-> cpp/code_formatting("`Code Formatting`") subgraph Lab Skills cpp/variables -.-> lab-96188{{"`计算 GCD 和 LCM`"}} cpp/conditions -.-> lab-96188{{"`计算 GCD 和 LCM`"}} cpp/for_loop -.-> lab-96188{{"`计算 GCD 和 LCM`"}} cpp/output -.-> lab-96188{{"`计算 GCD 和 LCM`"}} cpp/user_input -.-> lab-96188{{"`计算 GCD 和 LCM`"}} cpp/files -.-> lab-96188{{"`计算 GCD 和 LCM`"}} cpp/code_formatting -.-> lab-96188{{"`计算 GCD 和 LCM`"}} end

设置你的项目目录

首先,使用 mkdir 命令创建一个新文件夹,并使用 cd 命令在该目录中设置一个 C++ 项目。例如:

mkdir ~/project
cd ~/project

创建一个新的 C++ 文件

使用你喜欢的文本编辑器创建一个名为 main.cpp 的新文件。例如:

touch main.cpp

编写 C++ 代码

将以下 C++ 代码复制并粘贴到你的 main.cpp 文件中:

#include <iostream>
using namespace std;

int main()
{
    cout << "\n\nWelcome to the GCD and LCM calculator!\n\n\n";
    cout << "===== Program to find the GCD and LCM of two numbers =====\n\n";

    //variable declaration
    int n1, n2, i;

    //variable declaration and initialization
    int gcd = 1, lcm = 1;

    //taking input from the command line (user)
    cout << "Enter two numbers to find their GCD and LCM (separated by a space): \n\n";
    cin >> n1 >> n2;

    //logic to calculate the GCD and LCM of the two numbers
    for ( i = 1; i <= n1 && i <= n2; i++)
    {
        //i is the least value that perfectly divides both the numbers and hence the GCD
        if ((n1 % i == 0) && (n2 % i == 0))
        {
            gcd = i;
        }
    }

    lcm = (n1 * n2) / gcd;

    //print the GCD and LCM of the two numbers on the command line
    cout << "\nThe GCD of the two numbers " << n1 << " and " << n2 << " is : " << gcd << endl;
    cout << "\nThe LCM of the two numbers " << n1 << " and " << n2 << " is : " << lcm << endl;
    cout << "\nThanks for using GCD and LCM calculator!\n\n\n";

    return 0;
}

编译并运行代码

要编译并运行代码,请在终端中使用以下命令:

g++ main.cpp -o main && ./main

完整代码

以下是完整的代码,集中展示:

#include <iostream>
using namespace std;

int main()
{
    cout << "\n\nWelcome to the GCD and LCM calculator!\n\n\n";
    cout << "===== Program to find the GCD and LCM of two numbers =====\n\n";

    //variable declaration
    int n1, n2, i;

    //variable declaration and initialization
    int gcd = 1, lcm = 1;

    //taking input from the command line (user)
    cout << "Enter two numbers to find their GCD and LCM (separated by a space): \n\n";
    cin >> n1 >> n2;

    //logic to calculate the GCD and LCM of the two numbers
    for ( i = 1; i <= n1 && i <= n2; i++)
    {
        //i is the least value that perfectly divides both the numbers and hence the GCD
        if ((n1 % i == 0) && (n2 % i == 0))
        {
            gcd = i;
        }
    }

    lcm = (n1 * n2) / gcd;

    //print the GCD and LCM of the two numbers on the command line
    cout << "\nThe GCD of the two numbers " << n1 << " and " << n2 << " is : " << gcd << endl;
    cout << "\nThe LCM of the two numbers " << n1 << " and " << n2 << " is : " << lcm << endl;
    cout << "\nThanks for using the GCD and LCM calculator!\n\n\n";

    return 0;
}

总结

恭喜你,你已经学会了用 C++ 代码计算两个数的 GCD 和 LCM。掌握了这些技能后,你可以在各种应用中实现它们。

您可能感兴趣的其他 C++ 教程