teman- teman,kharm respond it with some simple mathematic operation.
Gini , gw cuma mo tau gimana caranya teman2 menyelesaikan soal berikut
Ada dua variabel a & b
a = 10;
b = 20;
Nah .. gimana caranya agar dua variabel itu ditukar nilainya tanpa menambah variabel tambahan
Bisa gak ya ? ....
yg lbih sederhana bisa pake operasi tambah kurang kaya gini:
a:= 10;b:=12
a:= a + b; {22}
b:= a - b; {10}
a:= a - b; {12}
Interesting, isnt it ?
versi oneliner:
ReplyDeletea ^= b ^= a ^= b;
lah ntu dah ada jwbnnya bos...
ReplyDeleteversi kharm..boljug tuh
fajran : interesting one :)
ReplyDeleteraindust : kali aja ente punya ide lain bos, biar otak ente ga bulukan, **kidding**; peace:peace
ReplyDeleteversi ruby:
ReplyDeletea, b = b, a
:)
anonymous : nice one
ReplyDeletekalo ga salah itu soal buat olimpiade komputer ya? iya kayaknya... :D
ReplyDeleteanonymous : wah ndak tau juga yah :), maybe yes .. maybe no :)
ReplyDeletehaha, mana ada soal olimpiade kayak gitu, toooo simple :p
ReplyDeleteanonymous: wah ndak tau juga bos, belun pernah ikut olimpiade :)
ReplyDeleteAnother trick is by using xchg instruction in x86 32/64 bit. Put it inside inline asm and we're done.
ReplyDeleteint main(int argc, char *argv[])
{
int a = 5;
int b = 10;
printf("a is %d\n",a);
printf("b is %d\n",b);
__asm__ __volatile__ ("xchg %0, %1":
"=r"(a),"=r"(b):
"0"(a),"1"(b)
printf("a is %d\n",a);
printf("b is %d\n",b);
return 0;
}
);