10940-throwing Cards away II
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ problem&problem=1881
Given is a ordered deck of n cards numbered 1 to n with card 1 in the top and cardn at the bottom. The following operation is performed as long as there are at least two cards in the deck:
Throw away the "top card" and "move" the "the" the "is" and "the" the "the" deck to the bottom of the deck.
Your task is to find the last, remaining card.
Each line of input (except the last) contains a positive number n≤500000. The last line contains 0 and the should is processed. For each number from input produce one line of output giving the last remaining card. Input won't contain more than 500000 lines.
Sample input
7
6
0
Output for sample input
6
6
4
4
Please refer to "specific mathematics"
Complete code:
/*0.039s*/
#include <cstdio>
int main ()
{
int n, tmp, CNT, ans;
while (scanf ("%d", &n), N)
{
cnt = 0;
for (TMP = n; tmp; tmp >>= 1) ++cnt;
Ans = (n-(1 << (cnt-1))) << 1;
printf ("%d\n", ans. ans:n);
return 0;
}
Author: csdn Blog Synapse7
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/