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

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

源代码:

#include<iostream>
using namespace std;
int main(){
	
	int x,y;
	cout<<"Input x and y:";
	cin>>x>>y;
	cout<<"x : "<<x<<", y : "<<y<<endl;
	if(x>y){
		y=x-y;
		x=x-y;
		y=x+y;
	}
	else{
		x=y-x;
		y=y-x;
		x=x+y;
	}
	cout<<"x : "<<x<<", y : "<<y<<endl;
	
	return 0;
} 

结果图:
1
2

发布了28 篇原创文章 · 获赞 51 · 访问量 5769

猜你喜欢

转载自blog.csdn.net/zhaizhaizhaiaaa/article/details/104114455