【转】不申请额外空间,交换A、B
http://www.cnblogs.com/liulunet/archive/2011/05/17/2049407.html
//不申请额外内存空间就可以完成数据互换的算法:int x=a,y=b;设计一个算法,不申请第三个变量,试把a b互换位置#include <iostream>using std::cin;using std::cout;using std::endl;int main(){ int x,y; cout << "Enter two numbers: X Y" << endl; cin >> x >> y; cout << "X = " << x << endl << "Y = " << y << endl; x = x+y; y = x-y; x = x-y; cout << "Through the exchange:" << endl << "X = " << x << endl << "Y = " << y << endl; return 0;}
浙公网安备 33010602011771号