zoukankan      html  css  js  c++  java
  • POJ 1012 Joseph

    Joseph
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 52102   Accepted: 19840

    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
    自己写的枚举超时的不得了
    借鉴了wmy大神的代码(虽然也超时,但是快多了):
     1 /*------------------------------分割线----------------------------------*/
     2 #include<iostream>
     3 using namespace std;
     4 #include<cstdio>
     5 int n,k,m;
     6 int main()
     7 {
     8     while(scanf("%d",&k)==1)
     9     {
    10         if(k==0) break;
    11         for(m=k+1;;m++)
    12         {
    13             n=2*k;
    14             int p=0;
    15             bool flag=true;
    16             for(int i=1;i<=k;++i)
    17             {/*我在这里用了暴力,挨个挨个的数*/
    18                 p=(p+m-1)%n;/*最巧妙的在于这句,p表示删除人在之前被删的序列(不是原序列)的编号,这个应该很好理解,每次人数减少,所以总人数n也--*/
    19                 if(p<k)/*如果我们删除的对的话,那么前k个人的编号是不变的,所以p始终是不能等于p的*/
    20                 {
    21                     flag=false;
    22                     break;
    23                 }
    24                 n--;
    25             }
    26             if(flag)
    27             {
    28                 printf("%d
    ",m);
    29                 break;
    30             }
    31         }
    32     }
    33     
    34     return 0;
    35 }
    36 /*------------------------------分割线----------------------------------*/
    37 打表代码:
    38 #include<iostream>
    39 using namespace std;
    40 int pp[15]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881};
    41 int main()
    42 {
    43     int k;
    44     while(cin>>k)
    45     {
    46         if(k==0) break;
    47         else cout<<pp[k]<<endl;
    48     }
    49     return 0;;
    50 }
  • 相关阅读:
    DSP Builder设计一个滤波器
    Modelsim 10.0 对Altera FFT IP 进行仿真
    FPGA内部计算小数
    TIOBE 2012年3月编程语言排行榜:JS超越Perl和Python
    转载 10个新鲜的Ajax相关的jQuery插件
    转载 使用HTML5、CSS3和jQuery增强网站用户体验
    转载 Java堆内存的10个要点
    累 腾讯笔试
    python 浮点数取整
    转载 一个页面重构工程师眼中的“用户体验”
  • 原文地址:https://www.cnblogs.com/c1299401227/p/5516325.html
Copyright © 2011-2022 走看看