一道Java算法題
有100個人圍成一個圈(編號0-99),從第0號的人開始從1報數,凡報到3的倍數的人離開圈子,然后再數下去,直到最后只剩一個人為止,問此人原來的位置是多少號?
- class Test
- {
- public static void main(String[] args)
- {
- int count = 100;
- //剩余人數
- int leavings = count;
- int[] all = new int[count];
- //index人物編號,counter報數
- int index = 0, counter = 0;
- while (true)
- {
- //人物對應的值為-1表示出局
- if (all[index] != -1)
- {
- all[index] = index+1;
- counter++;
- }
- if (counter%3 == 0)
- {
- if (--leavings == 1)
- {
- System.out.println(index);
- break;
- }
- all[index] = -1;
- }
-
- index++;
- if (index == count-1)
- {
- index = 0;
- }
- }
- }
- }
本文由用戶 quguiliang 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!