Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157254
  • 博文数量: 35
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-09 21:36
文章存档

2010年(10)

2009年(25)

我的朋友

分类:

2010-12-22 17:15:54

Joseph
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 32724Accepted: 12231

Description

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. 

Input

The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.

Output

The output file will consist of separate lines containing m corresponding to k in the input file.

Sample Input

3
4
0

Sample Output

5
30

Source



//POJ_1012

#include <stdio.h>

int main(void)
{
    int k = 0;        //good guys number

    int guys = 0;    //all guys

    int p = 0;        //now position

    int m = 0;        //looper

    int result[14];    //array of result

    int loop;

    for(loop=0; loop<14; loop++){
        result[loop] = 0;
    }
    
    while(1){
        scanf("%d", &k);
        if(k == 0) break;    //end of input

        m = k + 1;            //every m<=k is impossible

        while(result[k]==0){
            p = 0;
            guys = k * 2;
            while(guys > k){
                p = (p + m - 1) % guys + 1;

                if(p>k){    //the guy killed is a bad guy

                    guys--;
                    p--;
                }
                else
                 break;                
            }
            if(guys == k){
                result[k] = m;
             break;
            }
            m++;
        }
        printf("%d\n", result[k]);
    }
    return 0;
}

注:此题主要考察取模运算,但是特别容易超时,所以需要一个数组来记录已经计算出来的结果。
阅读(1085) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-12-23 14:35:35

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com