package second;import java.util.ArrayList;public class second {/** * 小朋友數數位遊戲 多個小朋友圍成一圈,依次數數字,當數到指定數字是就退出,繼續下一個從1開始數,如此迴圈 return 求出最後一個留下的小朋友 * * @param args */public static void main(String[] args) {// TODO Auto-generated method stub// 指定的數字int num = 4;// 指定數目的小朋友int person[] = { 1, 2, 3, 4, 5 };// 從第幾個小朋友開始數int beginperson = 1;//需要刪除的數組下標int count=beginperson-1;ArrayList<Integer> ai = new ArrayList<Integer>();for (int i = 0; i < person.length; i++) {ai.add(person[i]);}int numperson = CounGameMethod(num, ai, beginperson,count);System.out.println("第" + numperson + "留了下來");}private static int CounGameMethod(int num, ArrayList<Integer> person, int beginperson,int count) {if (person.size() != 1) {if(beginperson==num){System.out.println("第"+person.remove(count)+"個小朋友被淘汰");if(count==person.size()){count=0;}beginperson=1;}else{beginperson++;if(count<person.size()-1){count++;}else{count=0;}}return CounGameMethod(num, person, beginperson, count);} else {return person.get(0);}}}