题目描述:不用临时变量,直接交换两个数
思路
a = a+b;b = a- b;a = a-b
1 #include <iostream> 2 #include <queue> 3 #include <climits> 4 #include <algorithm> 5 #include <memory.h> 6 #include <stdio.h> 7 #include <ostream> 8 #include <vector> 9 #include <list> 10 #include <cmath> 11 #include <string> 12 #include <stdexcept> 13 #include <stack> 14 #include <map> 15 using namespace std; 16 17 template<typename T> 18 void fun(T &a,T &b) 19 { 20 a = a+b; 21 b = a-b; 22 a = a-b; 23 } 24 25 int main() 26 { 27 int a = 1; 28 int b = 2; 29 cout<<a<<" "<<b<<endl; 30 fun(a,b); 31 cout<<a<<" "<<b<<endl; 32 return 0; 33 }