First of all, LET me clarify the title, I am the little white, I understand (● flowed flowed ●)
Joseph’s ring is an applied problem of mathematics: given n people (numbered 1,2,3… Sit around a round table. Start with the number k and count off the number m; His next person counts off from 1, and the one who counts to m is out again; This pattern is repeated until all the people around the table are in line. Usually when we solve these problems we number them from 0 to n minus 1, and the result +1 is the original solution.
If you still do not understand the problem, Brother Yang Fan simple understandable Joseph ring
PS:
First of all, I’m going to attach a code that’s n people, k people, and I’m going to ask you what’s left
import java.util.Scanner; Public class Josephs ring {public static void main(String[] args) {Scanner in = new Scanner(system.in); int n = in.nextInt(), k = in.nextInt(); int p = 0; for (int i = 2; i <= n; i++) { p = (p + k) % i; } System.out.println(p + 1); }}Copy the code
The original question was what is the number of the last person left, so you can think of it this way
In normal logic, the last person to be eliminated,
So in that case, let’s think about it the other way around,
We could be the first one,
Okay, let's get back to business
This person must have been over,
\
So we start with two people, and we keep adding people in,
So the idea is, we start with the second person, and we count k people at a time,
Then every time % I number of people (currently I individuals), because if you exceed the current number of people, you have to count from the beginning
Our p holds the position,
And then you keep going, adding one person at a time
Because our p starts at 0, we end up with a +1 operation
In the process, we don’t have to worry about deleting the same people,
We’re pushing it to the side,
We start with n people, and every time we delete k people,
In the process, although we may have deleted the same person each time,
He can’t be the same person, because when he counts, he’s out,
By the time you count again, he’s been replaced by someone else
Finally, let’s conclude:
Positive: every time the number of people in position K exits, the number of people is -1
Reverse: +1 per person; Count up to K (if more than the current number, then % of the current number, that is, continue counting from the beginning)
Joking, ha ha, there are not understand, or xiaobian description of the wrong place, welcome to comment
\