出圏問題

package com.mianshi;

import java.util.LinkedList;
import java.util.List;

/**
 * describe: 出圏問題:N個人坐成一圈,從某一個人開始數,數到第I個人出圈,求最從出圈的人
 * create on: 2011-05-25
 * @author sylor.liu
 * @version 1.0
 * @since jdk1.6
 */
public class linklistuse {
 
 @SuppressWarnings("unchecked")
 public static int popStr(int maxInt, int cycInt){
  List ll = new LinkedList<Integer>();
  
  // 初始化鏈表
  for (int i = 0; i < maxInt; i++) {
   ll.add(i+1);
  }
  
  int index  = -1;
  while(ll.size() > 1){
   index = (index + cycInt)%ll.size();
   
   // 第cycInt個人出圈
   ll.remove(index--);
  }
  
  return (Integer)ll.get(0);
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println(linklistuse.popStr(10,3));
 }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章