1.三变量法
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int a, b, t;
cout << "输入两个整数a和b:";
cin >> a >> b;
cout << "交换前:a = " << a << ", b = " << b << endl;
t = a;
a = b;
b = t;
cout << "交换后:a = " << a << ", b = " << b << endl;
return 0;
}
运行结果:
2.加减法
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "输入两个整数a和b:";
cin >> a >> b;
cout << "交换前:a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a