C++不借助第三个变量交换两个变量的值

#include<iostream>
using namespace std;
int main()
{
	int a, b;
	cout << "inPut a and b:" << endl;
	cin >> a >> b;
	cout << "a:" << a << endl;
	cout << "b:" << b << endl;

	if (a > b)
	{
		b = a - b;
		a = a - b;
		b = a + b;
	}
	else
	{
		b = b - a;
		a = a + b;
		b = a - b;
	}
	cout << "a:" << a << endl;
	cout << "b:" << b << endl;
	return 0;
}

  

posted @ 2020-09-25 16:57  不妨不妨,来日方长  阅读(315)  评论(0编辑  收藏  举报