欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

出圈算法

程序员文章站 2022-06-08 09:50:16
...

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();
	}
}

相关标签: 出圈