脑筋急转弯 [英] brain teasers

查看:71
本文介绍了脑筋急转弯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用第三个变量的情况下交换两个变量?

how can two variables be swapped without using a third variable?

推荐答案



viv342写道:

viv342 wrote:

如何在不使用第三个变量的情况下交换两个变量?
how can two variables be swapped without using a third variable?



查找按位XOR运算符的作用。但编译器很可能无论如何都要使用临时的,并且它几乎总是更具可读性。

Lookup what the bitwise XOR operator does. But the compiler is likely
to use a temporary anyway, and it''s nearly always more readable also.


On 4月4日,11:42,viv342 < viv ... @ gmail.comwrote:
On 4 Apr, 11:42, "viv342" <viv...@gmail.comwrote:

如何在不使用第三个变量的情况下交换两个变量?
how can two variables be swapped without using a third variable?



做自己的作业,孩子。


提示:从维基百科中找到它不需要花费十秒钟

主页。

Do your own homework, kid.

Hint: It took no more than ten seconds to find it from the Wikipedia
home page.


viv342写道:
viv342 wrote:

how可以在不使用第三个变量的情况下交换两个变量吗?
how can two variables be swapped without using a third variable?



在发布这个陈腐问题之前你没有查看常见问题,是吗?

你也没检查档案,是吗? ?

当你回到认为愚蠢的XOR技巧整洁的白痴时,

问他对非整数变量做了什么。并问他如何知道编译器没有生成隐藏的第三个变量,即使他/ b $ b使用带有整数类型变量的愚蠢的XOR技巧。如果他给你一个单行答案

,那肯定会违反约束,即使是整数

变量。学习一些真正的编程,然后把这些废话留给那些用b / b
下注酒吧的人。

You didn''t check the FAQ before posting this trite question, did you?
Nor did you check the archives, did you?
When you go back to the idiot that thinks the stupid XOR trick is neat,
ask him what he does with non-integer variables. And ask him how he
knows the compiler doesn''t generate a hidden third variable even when he
uses the stupid XOR trick with integer type variables. And if he gives
you a one-line answer, it is sure to violate constraints even on integer
variables. Learn some real programming and leave the crap to people who
place bar bets for drinks.


这篇关于脑筋急转弯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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