交换两个数字 [英] swap two numbers

查看:108
本文介绍了交换两个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这一直是一个有趣的话题。你可以使用temp(作为第三个变量)交换两个

数字(比如a和b)。你也可以在不使用第三个变量的情况下交换

。[a = a + b; b = a-b; a = a-b; ]这也是在三个陈述中完成的。任何人都可以建议交换任何东西

少于那个。

hello all,
this has been an interesting topic since long. you can swap two
numbers (say a and b) using temp(as third variable). you can also swap
without using third variable.like[ a=a+b;b=a-b;a=a-b; ]This was also
done in three statements. Can anyon suggest this swapping in anything
less then that.

推荐答案

Abhishek Jha写道:
Abhishek Jha wrote:
这一直是一个有趣的话题。你可以使用temp(作为第三个变量)交换两个
数字(比如a和b)。你也可以交换
而不使用第三个变量。像[a = a + b; b = a-b; a = a-b; ]这也是在三个陈述中完成的。任何人都可以建议交换任何东西
少于那个。
this has been an interesting topic since long. you can swap two
numbers (say a and b) using temp(as third variable). you can also swap
without using third variable.like[ a=a+b;b=a-b;a=a-b; ]This was also
done in three statements. Can anyon suggest this swapping in anything
less then that.




a = a-(b =(a = a + b)-b);


-

如果每个人都读到USENET会更好:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html



a = a-(b=(a=a+b)-b);

--
USENET would be a better place if everybody read:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html




" Abhishek Jha" < AB *********** @ yahoo.co.in>在消息中写道

news:60 ************************** @ posting.google.c om ...

"Abhishek Jha" <ab***********@yahoo.co.in> wrote in message
news:60**************************@posting.google.c om...
大家好,
这一直是一个有趣的话题。你可以使用temp(作为第三个变量)交换两个
数字(比如a和b)。你也可以交换
而不使用第三个变量。像[a = a + b; b = a-b; a = a-b; ]这也是在三个陈述中完成的。任何人都可以建议交换任何东西
少于那个。
hello all,
this has been an interesting topic since long. you can swap two
numbers (say a and b) using temp(as third variable). you can also swap
without using third variable.like[ a=a+b;b=a-b;a=a-b; ]This was also
done in three statements. Can anyon suggest this swapping in anything
less then that.




[a = a + b; b = a-b; a = a-b;由于可能的
溢出,交换两个数字的方法很糟糕。不使用临时变量的更好方法是使用

XOR技巧[*](也适用于非整数类型)。但是,最好的方式可能只是使用标准的临时变量,让

编译器进行优化。

[*] a ^ = b; b ^ = A; a ^ = b;



[ a=a+b;b=a-b;a=a-b; ] is a bad way to swap two numbers due to possible
overflow. A better method without using a temporary variable is to use the
XOR trick[*] (which works for non-integral types as well). But, the best
way is probably just to use a standard temporary variable and let the
compiler do the optimization.
[*] a^=b; b^=a; a^=b;




Pedro Graca写道:

Pedro Graca wrote:
Abhishek Jha写道:
Abhishek Jha wrote:
这一直是一个有趣的话题。你可以使用temp(作为第三个变量)交换两个
数字(比如a和b)。你也可以在不使用第三个变量的情况下交换
。[a = a + b; b = a-b; a = a-b; ]这是
也在三个陈述中完成。任何人都可以建议在
之间交换任何东西。
this has been an interesting topic since long. you can swap two
numbers (say a and b) using temp(as third variable). you can also swap without using third variable.like[ a=a+b;b=a-b;a=a-b; ]This was also done in three statements. Can anyon suggest this swapping in anything less then that.



a = a-(b =(a = a + b)-b);



a = a-(b=(a=a+b)-b);




这比原始的愚蠢更糟糕,因为它总是调用

未定义的行为。


Rob Gamble



This is even worse than the original silliness as it always invokes
undefined behavior.

Rob Gamble


这篇关于交换两个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆