匿名用户
1级
2015-06-27 回答
以下是完整示例代码,countThree(50, 0, 3);的意思是50个人,从第1个位置起,每3个出一个。第二个参数就是你的问题的解答。
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
countThree(50, 0, 3);
}
/**
* @param n 人的总数
* @param start 开始报数的序号,start < n
* @param m 出列的标记(可以大于n)
*/
private static void countThree(int n, int start, int m) {
List list = new ArrayList();
//初始化列表
for (int i = 1; i <= n; i++) {
list.add(i);
}
while (list.size() > 0) {
//将前连个移入列表尾端
for (int j = 0; j < m-1; j++) {
list.add(list.remove(start));
}
//打印出列的序号
System.out.println(list.remove(start));
}
}
}