约瑟夫环问题:有17个人围成一个圈,编号是0~16,从0号的人开始从1开始报数,凡报到3的倍数的人离开圈子,然后再数下去,直到最后只剩一个人为止。输出最后剩下的一个人(输出此人原来的位置编号)。
求详解

解决方案 »

  1.   

     public class Josephas       //从第start人开始计数,以alter为单位循环记数出列,总人数为total      
       {  public static void Jose(int total, int alter, int start)         
           {                 
                int i, j, k = 0;            //count数组存储按出列顺序的数据,以当结果返回         
               int[] count = new int [ total+1];               //s数组存储初始数据          
               int[] s = new int [ total+1];                //对数组s赋初值,第一个人序号为0,第二人为1,依此下去          
               for (i = 0; i <total; i++)              
               {              
                   s[i] = i;         
               }            //按出列次序依次存于数组count中          
               for (i = total; i >= 2; i--)              
               {              
                   start = (start + alter - 1) % i;             
                   if (start == 0)         
                       start = i;             
                   count[k] = s[start];       
                   k++;              
                   for (j = start + 1; j <= i; j++)             
                       s[j - 1] = s[j];         
               }                       
               count[k]=s[1];            //结果返回           return count;        
           }
       }