在ruby中交换两个整数:
b ^= (a ^= b)
a ^= b
或:
a = a + b
b = a - b
a = a - b
左右:
$resclients=$mysqli->query("SELECT id,client_name FROM clients WHERE id = IN ($result)");
有没有更多的方法来交换两个整数而不创建第三个?
答案 0 :(得分:1)
评论的答案汇编。欢迎所有人添加他们的答案。
a = b.tap { b = a }
eval("a = #{b}; b = #{a}")
a = b + 0 * (b = a)
答案 1 :(得分:0)
有没有更多的方法来交换两个整数而不创建第三个?
Integer
此代码中只有两个b ^= (a ^= b)
# ^^^^^^ this creates a third integer
a ^= b
a = a + b
# ^^^^^ this creates a third integer
b = a - b
a = a - b
,没有创建第三个。{/ p>
请注意,此处发布的一些示例未通过此标准:
01-17 09:49:41.080 19462-19462/app.myappname E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: app.myappname.SplashActivity$1$1
at app.myappname.SplashActivity$1.onPostExecute(SplashActivity.java:180)
at app.myappname.SplashActivity$1.onPostExecute(SplashActivity.java:135)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:174)
at android.app.ActivityThread.main(ActivityThread.java:4952)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)