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

POJ 1781 In Danger

时间:2015-01-24 06:48:03      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:约瑟夫

题目链接:http://poj.org/problem?id=1781


In Danger
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3623   Accepted: 1889

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

Source


题解:  约瑟夫问题^_^
    这道题的n 是根据xyez算出来的比如 xyez为 05e0  则n=5* 10^0 =5  xyez 为01e1 则 n= 1* 10^1 = 10
         题目的意思和 POJ3517(点我查看)  差不多,相当于 POJ3517  中的m和k都为  2  ~_~ 那是不是把POJ3517 
        稍微修改一下就可以呢。 哈哈,还没这么简单。之前那道题 我是用递归写的,放在这里的话会爆栈,
为这题的数据比较大,不信可以试试 66e6 。
         这题蛮有意思的。我开始在草稿纸上吧n 等于1 2 3...的情况都算出来了,发现貌似有规律。于是直接用那个
          递归的代码,来求证(递归在数据比较小的时候还是可以的)


下面是我算的
技术分享

当n 为2^x 时 对应的输出都为 1          2^x-1 对应的值都为2^x-1  

对于一个数n 当  2^x < =   n  <2^x-1  则n对应的输出为 (n-2^x)*2+1   在纸上算算就可以推出来了^_^

AC 代码:
#include<iostream>
#include<cmath>
using namespace std;
int x,y,z,n;
char ch;
int main()
{
    while(cin>>x>>ch>>z){
        if(!x&&!z)break;
        n=x*pow(10.0,z);
        int pos=log((double)n)/log(2.0);
        n-=pow(2.0,pos);
        n=n*2+1;
        cout<<n<<endl;
    }
    return 0;
}

再附上一个爆栈的递归代码
#include<iostream>
#include<cmath>
using namespace std;
int x,y,z,n;
char ch;
int Ring(int cnt,int num){
    int temp;
    if(cnt==n){
        temp=(num+2)%cnt;
        if(temp)return temp;
        else return cnt;
    }
    temp=(num+2)%cnt;
    if(!temp)temp=cnt;
    return Ring(cnt+1,temp);
}
int main()
{
    while(cin>>x>>ch>>z){
         if(!x&&!z)break;
        n=x*pow(10.0,z);
        cout<<Ring(2,1)<<endl;
    }
}






POJ 1781 In Danger

标签:约瑟夫

原文地址:http://blog.csdn.net/mummyding/article/details/43077805

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