POJ 1781 In Danger

题目链接: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;
    }
}






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值