Joseph ring C # Solution

Source: Internet
Author: User

/* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.