Swap Three Numbers In Single Statement

2020-06-12 02:57发布

Is there any possiblty to swap three numbers in a single statement

Eg :

  • a = 10
  • b = 20
  • c = 30

I want values to be changed as per the following list

a = 20
b = 30
c = 10

Can these values be transferred in a single line?

标签: c logic
8条回答
三岁会撩人
2楼-- · 2020-06-12 03:49
$ python
>>> a, b, c = 10, 20, 30
>>> print a, b, c
10 20 30
>>> a, b, c = b, c, a
>>> print a, b, c
20 30 10
查看更多
SAY GOODBYE
3楼-- · 2020-06-12 03:51

I found another solution for this question.

You can use this in many languages like C,C++ and Java.

It will work for float and long also.

a=(a+b+c) - (b=c) - (c=a);
查看更多
登录 后发表回答