替换值 [英] replace values
问题描述
我正在使用2个整数X和Y.
我想替换那些值(Y = X,X = Y)但不使用任何其他
变量或函数。
Hrcko
10月3日上午10点22分, ; Hrvoje Voda" < hrvoje.vo ... @ zg.htnet.hrwrote:
我正在使用2个整数X和Y.
我想替换那些值(Y = X,X = Y)但不使用任何其他
变量或函数。
有一种非常黑客的做法,但使用临时变量
肯定会更简单,更具可读性。 />
我可以问*为什么*你不想使用任何其他变量或方法?
Jon
>
因为这是我必须解决的一个测试问题。
Hrcko
" ; Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言
news:11 ********************** @ d55g2000hsg.googlegr oups .com ...
10月3日上午10点22分,Hrvoje Voda < hrvoje.vo ... @ zg.htnet.hrwrote:
>我正在使用2个整数X和Y.
我想要替换那些值(Y = X,X = Y)但不使用任何其他
变量或函数。
有一种非常黑客的做法,但使用临时变量
肯定会更简单,更易读。 />
我可以问*为什么*你不想使用任何其他变量或方法?
Jon
x = x ^ y;
y = y ^ x;
x = x ^ y;
Graeme
尽一切努力摆脱愚蠢的世界!面试问题
" Hrvoje Voda" < hr ********** @ zg.htnet.hrwrote in message
news:fd ********** @ ss408.t-com.hr ...
因为这是我必须为工作解决的测试问题。
Hrcko
" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言
news:11 ********************** @ d55g2000hsg.googlegr oups .com ...
> 10月3日上午10点22分,Hrvoje Voda < hrvoje.vo ... @ zg.htnet.hrwrote:
>>我正在使用2个整数X和Y.
我想替换那些值(Y = X,X = Y),但不使用任何其他
变量或函数。
有一种非常黑客的做法,但使用临时变量
肯定会更简单,更具可读性。
我可以问*为什么*你不想使用任何其他变量或方法?
Jon
>
I''m using 2 integer numbers X and Y.
I would like to replace there values(Y=X, X=Y) but without using any other
variables or functions.
Hrcko
On Oct 3, 10:22 am, "Hrvoje Voda" <hrvoje.vo...@zg.htnet.hrwrote:I''m using 2 integer numbers X and Y.
I would like to replace there values(Y=X, X=Y) but without using any other
variables or functions.There''s a very hacky way of doing it, but using a temporary variable
would certainly be simpler and more readable.
Can I ask *why* you don''t want to use any other variables or methods?
Jon
Because it''s a test question that I have to solve for a job.
Hrcko
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...On Oct 3, 10:22 am, "Hrvoje Voda" <hrvoje.vo...@zg.htnet.hrwrote:>I''m using 2 integer numbers X and Y.
I would like to replace there values(Y=X, X=Y) but without using any
other
variables or functions.
There''s a very hacky way of doing it, but using a temporary variable
would certainly be simpler and more readable.
Can I ask *why* you don''t want to use any other variables or methods?
Jon
x = x ^ y;
y = y ^ x;
x = x ^ y;
Graeme
"Doing his bit to rid the world of idiotic aha! interview questions"
"Hrvoje Voda" <hr**********@zg.htnet.hrwrote in message
news:fd**********@ss408.t-com.hr...Because it''s a test question that I have to solve for a job.
Hrcko
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...>On Oct 3, 10:22 am, "Hrvoje Voda" <hrvoje.vo...@zg.htnet.hrwrote:>>I''m using 2 integer numbers X and Y.
I would like to replace there values(Y=X, X=Y) but without using any
other
variables or functions.
There''s a very hacky way of doing it, but using a temporary variable
would certainly be simpler and more readable.
Can I ask *why* you don''t want to use any other variables or methods?
Jon
这篇关于替换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!