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;
    }
  • 相关阅读:
    HashMap、HashTable与ConcurrentHashMap的区别
    HashMap的扩容机制---resize()
    jdk7中hashmap实现原理和jdk8中hashmap的改进方法总结
    HTML5学习总结——canvas绘制象棋(canvas绘图)
    ShardingJdbc:水平切分
    ShardingSphere:基本概念
    KubeSphere:harbor+gitlab+nexus+springboot流水线部署
    k8s+jenkins:部署SpringCloud微服务
    k8s实现Jenkins的Master-Slave分布式构建
    windows下jar包转成exe执行文件
  • 原文地址:https://www.cnblogs.com/jackge/p/3035140.html
Copyright © 2011-2022 走看看