設有n個人圍成一圈,從第一個人開始報數,數到第m個人出列,然後從出列的下一個人開始報數...
阿新 • • 發佈:2019-01-02
Java程式設計實現:設有n個人圍成一圈,從第一個人開始報數,數到第m個人出列,然後從出列的下一個人開始報數,數到第m個人又出列,...,如此反覆到所有人出列為止。設n個人的編號為1到n,打印出出列的順序。
序。
package test; public class Test { public static void main(String[] args) { play(10,7); } private static boolean same(int[] person,int l,int n) { for (int i=0; i<l; i++) { if(person[i] == n) { return true; } } return false; } public static void play(int playerNum, int step ) { int[] person = new int[playerNum]; int counter = 1; while(true) { if(counter > playerNum*step) { break; } for(int i=1;i<=playerNum;i++) { while(true) { if(same(person,playerNum,i) == false) { break; }else { i = i+1; } } if(i > playerNum) { break; } if(counter%step == 0) { System.out.println(i+" "); person[counter/step -1] = i; } counter +=1; } } System.out.println(); } }
序。