不使用临时变量交换两个数字

C++C++Beginner
立即练习

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

介绍

交换两个数字意味着它们的值应该互换,即第一个数字的初始值现在应该是第二个数字的值,反之亦然。在这个实验中,我们将学习两种不使用第三个变量来交换两个数字的方法:


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp(("`C++`")) -.-> cpp/AdvancedConceptsGroup(["`Advanced Concepts`"]) cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") cpp/AdvancedConceptsGroup -.-> cpp/references("`References`") 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-96217{{"`不使用临时变量交换两个数字`"}} cpp/operators -.-> lab-96217{{"`不使用临时变量交换两个数字`"}} cpp/function_parameters -.-> lab-96217{{"`不使用临时变量交换两个数字`"}} cpp/references -.-> lab-96217{{"`不使用临时变量交换两个数字`"}} cpp/output -.-> lab-96217{{"`不使用临时变量交换两个数字`"}} cpp/user_input -.-> lab-96217{{"`不使用临时变量交换两个数字`"}} cpp/files -.-> lab-96217{{"`不使用临时变量交换两个数字`"}} cpp/code_formatting -.-> lab-96217{{"`不使用临时变量交换两个数字`"}} end

创建一个新的 C++ 文件

打开你的终端,并使用以下命令导航到项目目录:

cd ~/project

使用以下命令创建一个名为 swap.cpp 的 C++ 文件:

touch swap.cpp

在你喜欢的代码编辑器中打开 swap.cpp

使用 + 和 - 运算符编写交换两个数字的代码

在这一步中,我们将编写使用 + 和 - 运算符交换两个数字的代码。代码应从用户那里获取两个数字,并在不使用第三个变量的情况下交换它们的值。

// swap.cpp
#include <iostream>
using namespace std;

void swap_numbers(int &x, int &y) {
    x = x + y;
    y = x - y;
    x = x - y;
}

int main() {
    int a, b;
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Before swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    swap_numbers(a, b);

    cout << "After swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

在这段代码中,我们创建了一个名为 swap_numbers 的函数,它接受两个整数参数并执行交换操作。我们从用户那里获取两个数字 ab,并调用 swap_numbers 函数来交换它们的值。最后,我们打印交换后的 ab 的值。

要运行程序,请使用以下命令:

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

使用 * 和 / 运算符编写交换两个数字的代码

在这一步中,我们将编写使用 * 和 / 运算符交换两个数字的代码。代码应从用户那里获取两个数字,并在不使用第三个变量的情况下交换它们的值。

// swap.cpp
#include <iostream>
using namespace std;

void swap_numbers(int &x, int &y) {
    x = x * y;
    y = x / y;
    x = x / y;
}

int main() {
    int a, b;
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Before swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    swap_numbers(a, b);

    cout << "After swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
 }

在这段代码中,我们同样创建了一个名为 swap_numbers 的函数,它接受两个整数参数并执行交换操作。我们从用户那里获取两个数字 ab,并调用 swap_numbers 函数来交换它们的值。最后,我们打印交换后的 ab 的值。在这种交换方法中,我们使用乘法和除法运算,而不是加法和减法。

要运行程序,请使用以下命令:

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

最终程序

以下是 swap.cpp 程序的完整代码,该程序通过加减法和乘除法两种方法交换两个数字。

// swap.cpp
#include <iostream>
using namespace std;

void add_subtract_swap(int &x, int &y) {
    x = x + y;
    y = x - y;
    x = x - y;
}

void multiply_divide_swap(int &x, int &y) {
    x = x * y;
    y = x / y;
    x = x / y;
}

int main() {
    int a, b;
    cout << "Enter the value of a: ";
    cin >> a;
    cout << "Enter the value of b: ";
    cin >> b;

    cout << "Before swapping: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    add_subtract_swap(a, b);

    cout << "After swapping using + and - operators: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    multiply_divide_swap(a, b);

    cout << "After swapping using * and / operators: " << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    return 0;
}

编译和运行

要编译并运行程序,请使用以下命令:

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

程序将要求用户输入两个数字,并通过加减法和乘除法两种方式交换它们的值。输出将显示 ab 的初始值、使用加减法交换后的值,以及使用乘除法交换后的值。

总结

交换是编程中的一项重要操作,许多算法中都需要用到它。在这个实验中,我们学习了两种不使用第三个变量交换两个数字的方法:使用加减法和使用乘除法。我们还学习了如何创建函数来执行交换操作,从而使代码更具模块化且更易于阅读。

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