Joseph (!)

#include <stdio.h>
#define MAX 27 
int next[MAX]; 
int pre[MAX]; 
int f(int k)
{
    int m;
    for(m=k+1;;m++) 
    { 
        //badGuys负责坏人的计数,pointer模拟指针在链表中移动 
        int badGuys=0,found=0,pointer; 
        //初始化模拟双向链表的数组,总共2*k个人 
        next[2*k]=1; 
        pre[1]=2*k; 
        for(int i=1;i<2*k;i++) 
        { 
            next[i]=i+1; 
            pre[i+1]=i; 
        } 
 
        for(int len=2*k,pointer=2*k;;len--) 
        { 
            int road=m%len; 
            if(road==0) 
                road=len; 
            while(road--) 
                pointer=next[pointer]; 
            if(pointer<=k) 
            { 
 
                break; 
            } 
            else
            { 
                badGuys++; 
                //删除所指坏人结点 
                int prePtr=pre[pointer]; 
                int nextPtr=next[pointer]; 
                next[prePtr]=nextPtr; 
                pre[nextPtr]=prePtr; 
            } 
            if(badGuys==k) 
            { 
                found=1; 
                break; 
            } 
        } 
        if(found) 
            break; 
    } 
    return m;
}
int main() 
{ 
    int k;
    int a[14];
        //答案存入数组,否则会超时
    for(int i=1;i<=13;i++)
        a[i]=f(i);
    while(scanf("%d",&k),k)
        printf("%d\n",a[k]);
 
}

 

#include <stdio.h>
/*
int x[] = { 2, 7, 5, 30, 169, 441, 1872, 7632, 1740, 93313, 459901, 1358657,  2504881 };
int main()
{
    int n;
    
    while(scanf("%d", &n),n)
    {    
        printf("%d\n", x[n-1] );    
    }
    return 0;
}*/
int test(int k,int m)
{
    int i,j=0,n=k<<1;
    for(i=0;i<k;i++)
    {
        j=(j+m-1)%(n-i);
        if(j<k) return 0;
    }
    return 1;
}

int main()
{    
    int i,k;
    int x[13];
    for(k=1;k<14;k++)
    {
        i=k+1;
        while(1)
        {
            if(test(k,i)) 
            {
                x[k-1]=i;
                break;
            }
            if(test(k,i+1))
            {
                x[k-1]=i+1;
                break;
            }
            i+=(k+1);
        }
    }

    while(scanf("%d",&k),k)
    {
        printf("%d\n",x[k-1]);
    }
    return 0;
}

 

posted @ 2013-09-19 15:47  Destino74  阅读(141)  评论(0)    收藏  举报