zoukankan      html  css  js  c++  java
  • POJ Joseph (蛋疼。。。。)

    Joseph

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
    Total Submission(s) : 0   Accepted Submission(s) : 0
    Problem 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
    PKU
     
     
     
    打表法:
     
    打表代码:
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int num[30],res[30];
    
    int isOK(int k){
        int t1=0,t2=0;
        for(int i=0;i<k;i++)
            t1+=num[i];
        for(int i=k;i<2*k;i++)
            t2+=num[i];
        if(t1==k && t2==0)
            return 1;
        return 0;
    }
    
    int Solve(int n,int m){
        int cnt=0,tmp=0;
        int i=0;
        while(1){
            if(num[i])
                tmp++;
            if(tmp==m){
                num[i]=0;
                tmp=0;
                cnt++;
            }
            i++;
            i%=n;
    
            if(cnt==n/2)
                break;
        }
        if(isOK(n/2))
            return 1;
        return 0;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    
        for(int k=1;k<14;k++){
            int m=k+1;
            while(1){
                for(int i=0;i<2*k;i++)
                    num[i]=1;
                if(Solve(2*k,m)){
                    break;
                }
                m++;
            }
            printf("%d,",m);
        }
        return 0;
    }


    再进行打表:

    #include<stdio.h>
    
    int num[14]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881};
    
    int main(){
        
        int k;
        while(~scanf("%d",&k) && k){
            printf("%d\n",num[k]);
        }
        return 0;
    }

    解法二:(另外一种打表)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    int res[20];
    
    void Init(){
        int sum;
        for(int k=1;k<14;k++)
            for(int m=k+1;;m++){
                sum=k*2;
                int flag=0;
                for(int j=m;;j+=m-1){
                    j=j%sum?j%sum:sum;
                    if(j<=k)
                        break;
                    sum--;
                    if(sum==k){
                        flag=1;
                        break;
                    }
                }
                if(flag){
                    res[k]=m;
                    break;
                }
            }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        Init();
        int k;
        while(~scanf("%d",&k) && k){
            printf("%d\n",res[k]);
        }
        return 0;
    }
  • 相关阅读:
    AWS的EC2实例搭建服务器使用stackoverflow教程
    亚马逊云开服之旅
    Linux主机之间ssh免密登录配置方法
    阿里云运维培训讲解
    区块链技术在物流领域应用分析
    公众号页面分享朋友圈后点击跳转到小程序的方法介绍
    数据库主库从库宕机重启后binlog数据同步
    Docker+ElasticSearch+Logstash+Kibana+Filebeat搭建方法
    linux实现磁盘自动挂载脚本
    阿里云ecs基于镜像进行旧服务器迁移到新服务器
  • 原文地址:https://www.cnblogs.com/jackge/p/3035140.html
Copyright © 2011-2022 走看看