/* Joseph's ring
(Problem description)
A description of Joseph's problem is that n people numbered 1, 2,... n sat around clockwise, and each person held a password (positive integer ). Select any one from the beginning
A positive integer is the upper limit m of the number of messages. The first person starts to report the number of messages in the order starting from 1 clockwise. The person reporting m is listed and his password is used as the new
The value of m, starting from 1 in the clockwise direction of the next person, so continue until all people are listed. Design a program to find out the column sequence.
(Basic requirements)
This process is simulated using the one-way cyclic linked list storage structure, and the individual numbers are printed in the column order.
(Test data)
The initial values of m are 20, and the passwords of n = 7 and 7 are respectively 3, 1, 7, 2, 4, and 4. First, m is 6 (the correct column sequence should be 6, 1, 2, 3, 5 ).
The following is my solution:
According to the problem, this is a ring problem. We can simulate a ring first.
Statement: I use a two-way linked list here. Although it does not make much sense to this problem, I still keep it as two-way, because I am considering this problem and may cause many other problems, only for my own use. Haha ...... To solve this problem, you can change it to a one-way linked list.
Public class Circle
{
Public class Node
{
Private Node preNode;
Private Node nextNode;
Private int id;
Private uint password;
Public Node PreNode
{
Get {
Return preNode;
}
Set {
PreNode = value;
}
}
Public Node NextNode {
Get {
Return nextNode;
}
Set {
NextNode = value;
}
}
Public int ID
{
Get {
Return id;
}
Set {
Id = value;
}
}
Public uint Password
{
Get {
Return password;
}
Set {
Password = value;
}
}
}
Private Node firstNode = null;
Private Node lastNode = null;
Private Node nextNode = null;
Private int count = 0;
Public int Count
{
Get {
Return count;
}
Set {
Count = value;
}
}
Public Circle ()
{
}
Public void Add (int id, uint password)
{
Count ++;
No