不使用第三个变量交换两个变量
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a;
cin>>b;
cout<<"交换前"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
a = a+b;
b = a -b;
a = a -b;
cout<<"交换后"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
return 0;
}
using namespace std;
int main()
{
int a,b;
cin>>a;
cin>>b;
cout<<"交换前"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
a = a+b;
b = a -b;
a = a -b;
cout<<"交换后"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
return 0;
}