码迷,mamicode.com
首页 > 其他好文 > 详细

acd The Game about KILL(和约瑟夫归则一样,归律)

时间:2014-05-25 10:02:35      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   c   code   

Problem Description

Teacher HU and his 40 students were trapped by the brigands. To show their power, the head of the brigands want to select one people to kill.
Teacher HU and his 40 students will stand in a circle, and every second person would leave, and the last people in the circle would be killed. For example, if there are 5 persons in the circle, counting proceeds as 2, 4, 1, 5 and person 3 will be killed. To make his students alive, teacher HU calculated the position to be the last man standing, and sacrifice himself.
Now we consider a more common condition, if teacher HU has N - 1 students, which place should he stand to be the last person.

Input

There are multiple test cases.
Each test case only contains an integer N. (1 <= N <= 1,000,000,000)

Output

For each test case, output an integer indicating which place should teacher HU stand.

Sample Input

2
3

Sample Output

1
3
题意:给出一个人数n,问以间隔2来去除,最后一个留下的是编号几。
归律:1:1  2:1  3:3  4:1  5:3  6:5  7:5  8:1  9:3  10:5  .......  可以看出输出的都是奇数,而且从2^k都是1,之后都是奇数递增。
#include<stdio.h>
int main()
{
    int n,tn,m;
    while(scanf("%d",&n)>0)
    {
        m=1; tn=n;
        while(tn>1)
        {
            m*=2; tn/=2;
        }
        printf("%d\n",2*(n-m)+1);
    }
}


acd The Game about KILL(和约瑟夫归则一样,归律),布布扣,bubuko.com

acd The Game about KILL(和约瑟夫归则一样,归律)

标签:des   style   class   blog   c   code   

原文地址:http://blog.csdn.net/u010372095/article/details/26824319

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!