How to swap two numbers without using temp variables or arithmetic operations?

后端 未结 10 997
Happy的楠姐
Happy的楠姐 2020-12-13 00:09

This equation swaps two numbers without a temporary variable, but uses arithmetic operations:

a = (a+b) - (b=a);

How can I do it without ar

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 00:45

    In addition to the above solutions for a case where if one of the value is out of range for a signed integer, the two variables values can be swapped in this way

    a = a+b;
    b=b-(-a);
    a=b-a;
    b=-(b);
    

提交回复
热议问题