出圈算法


50人围城一圈数到3和3的倍数是出圈,最后剩下的那位是谁
import java.util.LinkedList;
import java.util.List;

public class Cycle {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println(cycle(50,3));
	}

	private static int cycle(int total, int k) {
		List<Integer> dataList = new LinkedList<Integer>();
		for(int i = 0; i<total;i++){
			dataList.add(new Integer(i+1));
			
		}
		int index = -1;
		while(dataList.size()>1){
			index = (index+k)%dataList.size();
			dataList.remove(index--);
		}
		return dataList.get(0).intValue();
	}
}

你可能感兴趣的:(出圈)