OSX 10.9安装PyDev
用Python分割图片 (PIL)

不用第三个变量, 交换两变量的值 (C++)

kitt posted @ 2014年2月28日 17:22 in 技术类 Tech , 1626 阅读

解法还挺多的, 这里有3种。

1. 使用异或(XOR), a ^ a = 0

int a = 3, b = 7;
a = a ^ b;   // a = 3 ^ 7
b = a ^ b;   // b = 3 ^ 7 ^ 7 = 3
a = a ^ b;   // a = 3 ^ 7 ^ 3 = 7

 

2. 求和, 感谢elliot提供这种解法。

int a = 3, b = 7;
a = a + b;   // a = 10
b = a - b;   // b = 3
a = a - b;   // a = 7

 

3. 求差

int a = 3, b = 7;
a = b - a;   // a = 4
b = b - a;   // b = 3
a = b + a;   // a = 7
Avatar_small
elliot 说:
2014年3月04日 07:45

a = a + b;
b = a - b;
a = a - b;

Avatar_small
xy 说:
2014年3月10日 12:00

@kitt:
我是晓阳~~
这个是我当时BF的面试题,回来跟我说,我也是想到的是a=a+b, b=a-b,a=a-b
你这个我还没想到,我发给他看看哈哈

Avatar_small
kitt 说:
2014年3月10日 13:15

回复能看到吗? @xy:

Avatar_small
uulm 说:
2014年3月18日 10:42

void swap( int &r,int &s )
{
if( r < s )
{
--s;
swap( r,s );
++r;
}
else if( r > s )
{
--r;
swap( r,s );
++s;
}
}

Avatar_small
uulm 说:
2014年3月18日 10:46

void swap( int &r,int &s )
{
throw r;
catch (int what )
{
r = s;
s = what;
}
}

Avatar_small
elliot 说:
2014年3月24日 02:54

@kitt: 我向来只知道这一种解法...倒是要感谢你让我见识了更多的方法。

Avatar_small
AP 10th Science Ques 说:
2022年9月16日 13:08

AP 10th Class Science Model Paper 2023 Pdf Download may useful to both Telugu Medium, English Medium and Urdu Medium 10th class students of the state board to score better marks in the exams like SA-1, SA-2, FA-1, FA-2, FA-3, FA-4. AP 10th Science Question PaperThese practice model papers not only helped in getting marks but also improve the student’s knowledge of science.AP 10th Science Model Paper 2023 Pdf with answers suggested for all kinds of exams held under BSEAP along with assignments were made with the instructions of Leading educational institutes, Education portals of the state such as Sakshi Education, Eenadu Pratibha.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter