zoukankan      html  css  js  c++  java
  • Joseph(hdu1443)

    Joseph

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2126    Accepted Submission(s): 1291


    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

    0

     

    Sample Output
    5

    30

    思路:一开始看到数据很小,打表写的用vector容器模拟,这样模拟肯定超时,但由于只到13,所以,把数据打出来直接上表。

    另一种方法是只记录好人的开头和结尾,然后每次出一个人就更新开头和结尾,如果出来的人在头和尾之间就不可行。

    关键是更新开头和结尾: head=((((head-(p+1)%i)+1+i))%i); 

    wei=((((wei-(p+1)%i)+i+1))%i);(p+1)%i为去掉前面的数,然后开始的第一个数的原下标,然后算出head,与原下标之间的距离

    (head-(p+1)%i+i)%i,因为新的开头为1,所以加1就为当前标号head=((((head-(p+1)%i)+1+i))%i); 

     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<string.h>
     5 #include<math.h>
     6 #include<vector>
     7 void run();
     8 using namespace std;
     9 vector<int>my; int yy[14];
    10 int main(void)
    11 {
    12     int n,i,j,k,p,q;run();
    13     while(scanf("%d",&p),p!=0)
    14     {
    15         printf("%d ",yy[p]);
    16     }
    17     return 0;
    18 }
    19 void run()
    20 {
    21 
    22     int n,i,j,k,p,q;
    23     for(k=1;k<14;k++)
    24     {
    25         for(j=k+1;; j++)
    26         {
    27             int head=1;
    28             int wei=k;
    29             int sum=0;
    30             for(i=2*k; i>=1; i--)
    31             {   p=(j)%i;
    32                 if(p==0)
    33                     p=i;
    34                 if(p>=head&&p<=wei)
    35                     break;
    36                 sum++;
    37                 head=((((head-(p+1)%i)+1+i))%i);
    38                 wei=((((wei-(p+1)%i)+i+1))%i);
    39                 if(head==0)head=i-1;
    40                 if(wei==0)wei=i-1;
    41             }
    42             if(sum==k)
    43                 break;
    44         }
    45        yy[k]=j;
    46     }
    47 }


    油!油!you@
  • 相关阅读:
    会话技术——Cookie
    Servlet——Request和Response
    #Servlet——Web之间跳转和信息共享、三大作用域对象
    8个技巧教你区分LED灯具优劣
    建筑景观LED照明设计要考虑哪些?
    荧光材料物理特性对白光LED光输出冷热比的影响
    金刚战神D系列户外全彩D5.92
    2020爱你爱你,海佳集团祝您新年快乐!
    复制文本
    超市会员系统
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5182433.html
Copyright © 2011-2022 走看看