zoukankan      html  css  js  c++  java
  • In Danger_约瑟夫

    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 3720   Accepted: 1945

    Description

    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

    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

    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

    05e0
    01e1
    42e0
    66e6
    00e0
    

    Sample Output

    3
    5
    21
    64891137

    找规律

     1  2 3  4 5 6 7  8 9 10 11 12 13 14 15  16

     1  1 3  1 3 5 7  1 3 5 7 9 11  13 15 17   1

    当是2的幂次数时,答案为1,另外,比如3大于2^1,则f(3)=(3-2)*2+1;

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    using namespace std;
    int f[30];
    void solve()
    {
        f[0]=1;
        for(int i=1;i<=30;i++)
        {
            f[i]=f[i-1]*2;
        }
    }
    int main()
    {
        int n,m;
        char c;
        while(cin>>n>>c>>m)
        {
            int ans;
            solve();
            if(!n&&!m) break;
            n=n*pow(10.0,m);
            for(int i=0;i<=30;i++)
            {
                if(n==f[i]) {ans=1;break;}
                if(n<f[i]) {ans=2*(n-f[i-1])+1;break;}
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    ESP32 SDA和SCL
    ESP32的HSPI和VSPI区别
    ffmpeg生成视频封面图
    小程序读取几种不同格式json数据(小程序json解析)
    ajax
    使用Java语言,连接linux服务器,并远程执行shell 脚本
    Echarts饼图的使用
    js提取对象数组中的某一个属性
    java读取文件推送报文
    java读取本地文件内容TXT文件
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5814619.html
Copyright © 2011-2022 走看看