1 package day5; 2 3 import java.util.ArrayList; 4 /** 5 * 6 * 把犯人围成一圈,每次从固定位置开始算起,杀掉第7个人,直到剩下最后一个。 7 * 8 */ 9 10 public class YueSeFu { 11 12 public static void main(String[] args) { 13 int n = 20; 14 ArrayList aList = new ArrayList(); 15 for (int i = 1; i <= n; i++) { 16 aList.add(i); 17 } 18 int k = 0; 19 while(aList.size()>0){ 20 k = k+7; 21 k = k % (aList.size()) - 1; 22 if (k < 0) 23 { 24 System.out.println(aList.get(aList.size()-1)); 25 aList.remove(aList.size() - 1); 26 k = 0; 27 } 28 else 29 { 30 31 System.out.println(aList.get(k)); 32 aList.remove(k); 33 } 34 } 35 } 36 }