简单写了一个,你参考一下吧:
import java.util.*;
public class t
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("输入总人数:");
int n = sc.nextInt();//总人数
int count = 0;//记录出局的人数
int index = 0;//人的序号
int[] array = new int[n];
while(count != n - 1)
{
for(int i = 0;i < 3;i++)
{
if(array[index++] == 1)
{
i--;
}
if(index == n)
{
index = 0;
}
}
if(index == 0)
{
array[n-1] = 1;
}else{
array[index-1] = 1;
}
System.out.println((index==0?n:index)+"号出列");
count ++;
}
for(int i = 0;i < n;i++)
{
if(array[i]==0){
System.out.println("最后剩下的是" + (i+1) + "号");
}
}
}
}
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/cefc1e178a82b901e7f9a20e718da9773912efb3?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)