Joseph question java code

1:Joseph problem


Total time limit: 
1000ms 
Memory limit: 
65536kB
describe

There are n monkeys. They form a circle in a clockwise direction to choose the king (numbered from 1 to n). Start counting from number 1 and count to m. The monkeys who count to m exit the circle, and the remaining monkeys continue to count. Then start counting from 1. In this way, until there is only one monkey left in the circle, this monkey is the monkey king. The programming requires that after inputting n and m, the number of the last monkey king will be output.

enter
The input contains two integers, the first is n and the second is m (0 < m, n <=300).
output
The output contains one line, which is the number of the last monkey king.
Sample input
12 4
Sample output
1




import java.io.*;
import java.util.*;

public class Main {
	public static void main(String args[]) throws Exception {
		Scanner cin = new Scanner(System.in);
		int a = cin.nextInt(), b = cin.nextInt();
		int[] arr1 = new int[a];
		int cnt = 0;
		int index = 0;
		int i = 0;
		while (cnt < a - 1) {
			if (arr1[i % a] == 1) {
				i++;
				i %= a;
				continue;
			} else {
				if (index == b - 1) {
					cnt++;
					index = 0;
					arr1[i] = 1;
				} else {
					i++;
					i %= a;
					index++;
				}
			}
		}
		for (int j = 0; j < arr1.length; j++) {
			if (arr1[j] != 1)
				System.out.println(j + 1);
		}
	}
}



1

1

Guess you like

Origin blog.csdn.net/sunnyfirefox/article/details/48131139