The number-reporting game 1-3 loops to report the number, the person who reports to 3 exits, find the original serial number

topic:

There are n people in a circle, arranged in order. Start counting from the first person (from 1 to 3). Anyone who reports to 3
exits the circle and asks which person is the original number left.

import java.util.Scanner;
public class CountingGame {
    
    
	public static void main(String[] args)  {
    
    
		Scanner s = new Scanner(System.in);
		System.out.print("请输入排成一圈的人数:");
		int n = s.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)+"位的人留下了。");
			}
		}
	}
}

Guess you like

Origin blog.csdn.net/p715306030/article/details/113994740