|
发表于 2009-4-23 20:20:00
|
显示全部楼层
偶然再看到这个帖子,又偶然看到第3题,恰好是上个月无意逛POJ看到的。当时想了大约1小时吧,提交上去就AC了, 我可不是ACM选手,纯粹是为了好玩。
贴个我求杀人步长的解法,不是很好,恰好满足 k<=14 的POJ要求。
POJ那题描述和Iambitious兄的略微不同,简单来说就是一共有2k个人,要求杀k次,且每次都杀死排在最后的k个坏人之一,求杀人步长m- /*
- The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
- Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
- */
- #include <stdio.h>
- #define BOOL int
- #define TRUE 1
- #define FALSE 0
- #define M_MAX 15000000
- #define K_MAX 15
- BOOL pick(int k, int m)
- {
- int i;
- int currpos=0;
- int nextpos;
-
- for(i=0;i<k;i++)
- {
- nextpos=(currpos+(m-1)%(2*k-i)) % (2*k-i);
- if(nextpos<k) return FALSE;
- currpos=nextpos;
- }
-
- return TRUE;
- }
- int main()
- {
- int k;
- int m;
-
- for(k=1;k<=K_MAX;k++)
- {
- for(m=k+1;m<M_MAX;m++)
- {
- if(pick(k,m)==TRUE) break;
- }
-
- if(m<M_MAX)
- {
- printf("k=%d m=%d\n",k,m);
- }
- else
- {
- printf("m not found while k=%d, with limitation m<=%d\n",k, M_MAX);
- }
- }
- }
复制代码 |
|