java编程 有n个人围成一个圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出

java编程
有n个人围成一个圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,输出退出顺序。

public class Increase {

public static void rep(boolean[] people) {
int i = 0,j=0,n=people.length,m=n;
while(n>2){
i=++i%m;
if (people[i] == true){
j++;
if (j==3){
people[i] = false;
System.out.println(i);
n--;//总人数减1
j = 0;//到3从头数
}
}
}
}

public static void main(String[] args) {
boolean people[] = new boolean[10];
for(int i = 0; i<10; i++){
people[i] = true;
}
rep(people);
}
}

main函数为测试例子,打印结果如下

3

6

9

2

7

1

8

5

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-03
public static void main(String[] args)
{
int n=1;

boolean[] arr = new boolean[n]; // 创建一个布尔型数组
int contNum = 0; // 数数,计算数了多少个,数到3就退1个
int index = 0; // 第几个位置,数到的位置
int arrlength = arr.length; // 数字队列还有多少个
System.out.println(arr[arrlength-1]);
for (int i = 0; i < arr.length; i++)
{
arr[i] = true; // 把数组的值得全部置为true
}
while (arrlength > 1) // 留下最后一个
{
if (arr[index] == true)
{
contNum++; // 从1开始数
if (contNum == 3) // 从1开始的,数到3就是第三个数字了
{
arr[index] = false; // 数到第三个数,把它的值变为false
arrlength--; // 数组长度减去1
contNum = 0; // 重新数数
}
}
index++; // 每判断一个,加1
if (index == arr.length) // 判断是否到了最后的数字,到了,重新从0开始
{
index = 0;
}
}
// 打印数组中有true值的那个下标值,即最后那个是第几个
for (int j = 0; j < arr.length; j++)
{
if (arr[j] == true)
{
System.out.println("最后留下来的是第"+String.valueOf(j+1)+"个");
}
}
}
第2个回答  2015-06-03
这个问题貌似很经常遇到,看来大学编程老师题目都是一样的追问

怎么写↖( ̄▽ ̄")

追答

你到shx.io去发这个问题,我让我朋友帮你写,我不会java