最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

交换两个数字

SEO心得admin43浏览0评论
本文介绍了交换两个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 这一直是一个有趣的话题。你可以使用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会更好: www.expita/nomime.html wwwmeister/news/learn2quote2.html www.catb/~esr/faqs/smart-questions.html

a = a-(b=(a=a+b)-b); -- USENET would be a better place if everybody read: www.expita/nomime.html wwwmeister/news/learn2quote2.html www.catb/~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

发布评论

评论列表(0)

  1. 暂无评论