poj 1781

http://poj.org/problem?id=1781

大意:约瑟夫环,求最后剩下的位置是??该题为每两个划掉一个数。。eg:12345 划掉的为,2,4,1,5,最后剩下的为3.。。。

解法:直接模拟肯定会超,注意本题是以2为循环的。。可以尝试找规律(话说我就直接模拟的。。傻啊。。。)

   1-------1;

  2---------1;

3------------3;

4-----------1;

5---------3;

6-------5;

7---------7;。。。。。。。由此可见是以2的指数为增长的。。所以。。。

 1 #include <iostream>
 2 #include<cstring>
 3 using namespace std;
 4 char str[5];
 5 int f[99000009];
 6 int main()
 7 {
 8     while(cin>>str){
 9         if(!strcmp(str,"00e0"))
10             break;
11         int n;
12         n = (str[0]-'0')*10+(str[1]-'0');
13         for(int i=0;i<(str[3]-'0');i++)
14             n = n*10;
15         int c =1;
16         for(int i=1;;i++){
17             if(c>=n)
18                 break;
19             n = n-c;
20             c = c*2;
21         }
22         cout<<2*n-1<<endl;//最后就可直接得出
23     }
24     return 0;
25 }
原文地址:https://www.cnblogs.com/Bang-cansee/p/3241940.html