POJ 1012-Joseph

来源:互联网 发布:南京大学软件研究生院 编辑:程序博客网 时间:2024/05/16 13:38
Joseph
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 47657 Accepted: 17949

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

340

Sample Output

530
小结:
    这题目我还是很无语的,一开始就没意识到这是水题,打算用搜索来,想了半天总是不对,后面听别人说了说解法,真想跳楼...
以下是AC代码:
暴力解法:
#include <stdio.h>#include <string.h>int main(){    int k;    int ans[20];    int Joseph[20]={0};    for(k=1;k<14;k++)    {        int m=1;        memset(ans,0,sizeof(ans));        for(int i=1;i<=k;i++)        {            ans[i]=(ans[i-1]+m-1)%(2*k-i+1);            if(ans[i]<k)            {                i=0;                m++;            }        }        Joseph[k]=m;    }    while(scanf("%d",&k)&&k!=0)    {        printf("%d\n",Joseph[k]);    }    return 0;}
0ms解法:(呵呵,我感觉能用这种方法的人将来一定很有前途!!!!)#include <stdio.h>#include <string.h>int main(){    int k;  int Joseph[]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881,1245064};  while(scanf("%d",&k)&&k!=0)   {       printf("%d\n",Joseph[k]);   }   return 0;}
0 0
原创粉丝点击