m个人围成一圈,从第一个人开始数,每数到第六个数退出圈外。请依次打印出退出的人的号码!(如:数到第六个人,他退出,从第七个人开始数,依次数,直到全都退出)

解决方案 »

  1.   

    貌似只能用循环链表做
    以前是只要求最后1个人,可以用数学方法简化再用java编写   
    for(int i=2;i<=m;i++){
        r=(r+n)%i;
    }r+1就是最后个,n是第n个退出
      

  2.   

    这个题目叫做:约瑟夫环
    你可以baidu下
      

  3.   

    public class Ring{
        public static void main(String[] args){
            int M,N;
            M =20;
            N =6;
            boolean[] f =new boolean[M];
            int r =M, n =1, i =0;
            while(r>0){
                if(!f[i]){
                    if(n==N){
                        f[i] =true;
                        r --;
                        System.out.println("第"+(i+1)+"号被踢");
                        n =1;
                    }
                    else n++;
                }
                i++;
                if(i>=f.length) i=0;
            }
        }
    }
      

  4.   

    flushtime() 给的算法我看懂了,好经典的算法,很好,我再次收藏,
      

  5.   

    Algorithm Gossip: 約瑟夫問題(Josephus Problem)
    說明
    據說著名猶太歷史學家 Josephus有過以下的故事:在羅馬人佔領喬塔帕特後,39 個猶太人與Josephus及他的朋友躲到一個洞中,39個猶太人決定寧願死也不要被敵人到,於是決定了一個自殺方式,41個人排成一個圓圈,由第1個人開始報數,每報數到第3人該人就必須自殺,然後再由下一個重新報數,直到所有人都自殺身亡為止。然而Josephus 和他的朋友並不想遵從,Josephus要他的朋友先假裝遵從,他將朋友與自己安排在第16個與第31個位置,於是逃過了這場死亡遊戲。public class Josephus {    public static int[] arrayOfJosephus(                               int number, int per) {        int[] man = new int[number];         for(int count = 1, i = 0, pos = -1;                 count <= number; count++) {             do {                 pos = (pos+1) % number;  // 環狀處理                 if(man[pos] == 0)                     i++;                 if(i == per) {  // 報數為3了                     i = 0;                     break;                 }             } while(true);             man[pos] = count;         }                 return man;    }    public static void main(String[] args) {        int[] man = Josephus.arrayOfJosephus(41, 3);        int alive = 3;                System.out.println("約琴夫排列:");         for(int i = 0; i < 41; i++)             System.out.print(man[i] + " ");         System.out.println("\nL表示3個存活的人要放的位置:");         for(int i = 0; i < 41; i++) {             if(man[i] > alive)                 System.out.print("D");             else                 System.out.print("L");             if((i+1) % 5 == 0)                 System.out.print("  ");         }         System.out.println();    }}
      

  6.   

    public class A {
      public static void main(String[] args) {
        int m = 20, sx = m;
        int a[] = new int[m];
        for (int i = 1; i <= m; i++)
          a[i - 1] = 1;
        int i = 0, lj = 1;
        while (sx > 0) {
          i = ++i % m;
          if (a[i] != 0)
            lj++;
          if (lj == 6) {
            a[i] = 0;
            sx--;
            System.out.println(i + 1);
            lj = 0;
          }
        }
      }
    }