信号量用多了程序容易陷入死循环,这个写的有点复杂了,
import java.util.*;
class BaoShu {
public static void main(String[] args) {
while(true){
System.out.println("请输入n的值:");
Scanner s0 = new Scanner(System.in);
int n = s0.nextInt();
int shuzu[]=new int[n];
int i=0;
for(i=0;i<shuzu.length;i++){
shuzu[i]=i+1;
}
i=0;
while(n>1){
for (int j=1;j<=3;j++){
int k=1;
if(j==1){
while(k!=0){
if(shuzu[i]<=shuzu.length){
k=0;
}
i++;
if(i==shuzu.length){
i=0;
}
}
}
if(j==2){
while(k!=0){
if(shuzu[i]<=shuzu.length){
k=0;
}
i++;
if(i==shuzu.length){
i=0;
}
}
}
if(j==3){
while(k!=0){
if(shuzu[i]<=shuzu.length){
shuzu[i]+=shuzu.length;
k=0;
n--;
}
i++;
if(i==shuzu.length){
i=0;
}
}
}
}
}
for (i=0;i<shuzu.length ;i++){
if(shuzu[i]<=shuzu.length){
System.out.println("最后留下的是原来的第"+shuzu[i]+"位");
}
}
}
}
}