joj 2077: In Danger (約瑟夫環+位元運算初步)

來源:互聯網
上載者:User
文章目錄
  • Input Specification
  • Output Specification
  • Sample Input
  • Sample Output
Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 128 55 Standard
Flavius Josephus and 40 fellow rebels were trapped by the Romans. His companions preferred suicide to surrender, so they decided to form a circle and to kill every third person and to proceed around the circle until no one was left. Josephus was not excited by the idea of killing himself, so he calculated the position to be the last man standing (and then he did not commit suicide since nobody could watch).

We will consider a variant of this "game" where every second person leaves. And of course there will be more than 41 persons, for we now have computers. You have to calculate the safe position. Be careful because we might apply your program to calculate the winner of this contest!

Input Specification

The input contains several test cases. Each specifies a number n, denoting the number of persons participating in the game. To make things more difficult, it always has the format "xyez" with the following semantics: when n is written down in decimal notation, its first digit is x, its second digit is y, and then follow z zeros. Whereas 0<=x,y<=9, the number of zeros is 0<=z<=6. You may assume that n>0. The last test case is followed by the string 00e0.

Output Specification

For each test case generate a line containing the position of the person who survives. Assume that the participants have serial numbers from 1 to n and that the counting starts with person 1, i.e., the first person leaving is the one with number 2. For example, if there are 5 persons in the circle, counting proceeds as 2, 4, 1, 5 and person 3 is staying alive.

Sample Input
05e001e142e066e600e0
Sample Output
352164891137
//《具體數學》中,當m=2時,最後的人就是n左迴圈移位的值(某牛的Blog寫的是右移,但是他的code上明明是左移)
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main ()
{
    char ch[10];
    while (scanf("%s",ch)!=EOF)
    {
        int i,ans,n,e;
        n=10*(ch[0]-'0')+ch[1]-'0';
        e=ch[3]-'0';
        if(!(n||e))break;
        while (e--)
         n*=10;
        int c=1;
        while (c<=n)
         c<<=1;//移到n的前一位
        ans=((n-(c>>1))<<1)+1;//不知道為什麼
        printf("%d/n",ans);
    }
    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.