UVa 10940 throwing cards away II: Joseph Problem

Source: Internet
Author: User
Tags time limit

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.