Breaking the Magicians Code
All submissions for this problem are available.
You are walking on the streets of New York when the famous illusionist David Blaine comes up to you. He is holding an ordered deck of 10 cards numbered 1 to 10 with card 1 at the top and card 10 at the bottom. He gives the pack of cards to you. He asks you to throw away the top card and move the card that is now on the top of the deck to the bottom of the deck and to keep doing this as long as there are at least two cards in the deck. When you are left with the last card in your hand he takes a minute and then says 4. To your amazement when you turn the card over, it is 4. How does he do it?
Write a program to predict the last card when there are n cards in the ordered deck.
Each line of input (except the last) contains a positive number n. The last line contains 0 denoting the end inputs.
. For each number from input produce one line of output giving the last remaining card.
Input: 8 15 23 9 0 Output: 8 14 14 2
Note : The reference for this problem has been taken from : UVa online Judge
|Time Limit:||0.1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, CLOJ, FS|
Fetching successful submissions