c - Is it possible to swap two variables with only += and -= no other operators or variables? -
i want swap 2 variables += , -= operators , without temporary variables. know standard solutions like:
a = a+b; b = a-b; = a-b; and xor:
a ^= b; b ^= a; ^= b; but cannot figure out how += , -=. possible?
my classmates offer nice solution: answer no
lets denote a (1 0) , b (0 1)
the matrix a is
1 0 0 1 += , -= mean adding or subtracting lines each other. means determinant either not change sign or equal 0. end matrix is
0 1 1 0 with determinant equal -1, can not it
update: have these operations:
a-=a. 1 line becomes0,det=0a+=a. means multiplying line2,det a'= 2*det aa+=b. means elementary transformation, not change value ofdeta-=b. same thing3.
then apply proof b-=b, b+=b, b+=a, b-=a. determinant not change sign or 0
update 2: @tom said here example of how in c#: http://ideone.com/uzvlml. however, in c thats not correct: http://codepad.org/rmhn9iqb. can clarify difference of b -= b += b in c , c#?
Comments
Post a Comment