Java经典习题37

/*
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,
问最后留下的是原来第几号的那位。
*/

import java.util.*;

public class Class37 {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入总人数n:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean[] arr = new boolean[n];
for(int i = 0; i < arr.length; i++){
arr[i] = true;
}
int leftCount = n;
int countNum = 0;
int index = 0;
while(leftCount > 1){
if(arr[index] == true){
countNum++;
if(countNum == 3){
countNum = 0;
arr[index] = false;
leftCount--;
}
}
index++;
if(index == n){
index = 0;
}
}
for(int i = 0; i < n; i++){
if(arr[i] == true){
System.out.println("原排在第" + (i+1) + "位的人留下了。");
}
}
/*
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = i + 1;
}
for(int i = 0; i < n; i++){
System.out.print(a[i] + " ");
}
System.out.println();
int[] b = new int[n];
for(int i = 0; i < n; i++){
b[i] = 1;
}
int count = 1;
for(int i = 0; i < 1; i++){
for(int j = 0; j < n; j++){
if((count != 3) && (b[j] != 0)){
b[j] = a[j];
count++;
}
if((count == 3) && (b[j] != 0)){
b[j] = 0;
count = 1;
}
if((count != 3) && (b[j] == 0)){
//count--;
continue;
}
if((count == 3) && (b[j] == 0)){
//count--;
continue;
}
System.out.print(count);
}
}
System.out.println();
for(int i = 0; i < n; i++){
System.out.print(b[i] + " ");
}
*/

}

}

猜你喜欢

转载自www.cnblogs.com/zhuozige/p/12358775.html