隨筆-48  評論-26  文章-0  trackbacks-0
             問題:
                      有17個人圍成一圈(編號0~16),從第0號的人開始從1報數,凡報到3的倍數的人離開圈子,
                      然后再數下去,直到最后只剩下一個人為止,問此人原來的位置是多少號?

            
          int [] people = new int[17];
              
          int lastPeople = 0;
              
          public void getTheLastPeople(){       
                  
          for(int i = 0; i < people.length; i++){
                      people[i] 
          = i+1;
                  }
                  
          int count = 0;
                  
          int countLast = 0;
                  
          int j = 0;
                  
          while(true){
                      
          for(j = 0; j < people.length; j++){               
                          
          if(people[j] != 0){
                              count
          ++;
                              people[j] 
          = count;
                              System.out.println(
          "people[" + j + "] = " + people[j]);
                              
          if (people[j] % 3 == 0) {
                                  people[j] 
          = 0;
                                  countLast
          ++;
                                  
          if(countLast == 17){
                                      lastPeople 
          = j;
                                      
          return;
                                  }
                              }
                          }
                      }           
                  }
              }
          posted on 2009-09-26 14:35 Worker 閱讀(146) 評論(0)  編輯  收藏 所屬分類: 算法/數據結構

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          主站蜘蛛池模板: 潜山县| 深州市| 兰考县| 望奎县| 吴忠市| 望江县| 寻乌县| 博罗县| 如东县| 武强县| 罗江县| 临湘市| 都昌县| 黄大仙区| 莎车县| 横峰县| 平罗县| 鹿邑县| 平凉市| 大余县| 南充市| 明溪县| 清新县| 汤原县| 韶关市| 屏东市| 平乡县| 古浪县| 错那县| 太保市| 台东市| 南投县| 巩留县| 旬邑县| 武威市| 武定县| 黄梅县| 利津县| 宁城县| 墨竹工卡县| 屏山县|