sdut 2351 In Danger (找规律)

题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2351

题意:xyez, xy表示一个十进制数,z表示xy后面有几个0,这些个人成一个约瑟夫环,隔一个人杀一个人。。

求哪个位置上的人会幸存,用一般的方法会超时。

周赛的题,没做出来, 打表以后会找出来规律, 每2^n 都是1会幸存,

剩下的成1 2 3 1 3 5 7 1 3 5 7 9 11 13 15

这样的规律。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int i, a, b, len, sum;
11     int cnt;
12     char s[100];
13     while(~scanf("%s", s) && strcmp(s,"00e0")!=0)
14     {
15         b = 1;
16         a = (s[0] - 48)*10 + s[1] - 48;
17         len = s[3] - 48;
18         while(len--)
19         {
20             b *= 10;
21         }
22         a = a*b;
23         sum = 1;
24         for(i = 1; i <= 30; i++)
25         {
26             if(sum > a)
27             break;
28             sum *= 2;
29         }
30         sum /= 2;
31         cnt = 1;
32         for(i = sum; i < a; i++)
33         cnt += 2;
34         printf("%d\n", cnt);
35     }
36     return 0;
37 }

 

posted @ 2014-02-22 15:17  水门  阅读(216)  评论(0编辑  收藏  举报