/*有n個人圍成一圈,順序排號。從第一個人開始報數(從1到3報數),凡報到3的人退出圈子,問最後留下的是原來第幾號的那位*/ import java.util.Scanner; public class baoshu { public static void main(String[] args) { System.out.println("請輸入總人數n:"); Scanner s = new Scanner(System.in); int n = s.nextInt(); boolean[] arr = new boolean[n]; for(int i=0; i<arr.length; i++) { arr[i] = true;//值爲TRUE時說明還在圈裏 } int leftCount = n;//剩餘的人數,一開始爲總人數n int countNum = 0;//報的數字 int index = 0; //index下標值 while(leftCount > 1) { if(arr[index] == true) {//當在圈裏時 countNum ++; //報數遞加 if(countNum == 3) {//報道3時 countNum =0;//從零開始繼續報數 arr[index] = false;//此人退出圈子 leftCount --;//剩餘人數減一 } } index ++;//每報一次數,下標加一 if(index == n) {//是循環數數,當下標大於n時,說明已經數了一圈, index = 0;//將下標設爲零重新開始。 } } for(int i=0; i<n; i++) { if(arr[i] == true) { System.out.println("NO."+i);//循環求出值爲true的那個下標 } } } }
報數遊戲
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.