In a circle of 13 people, report the number of people reporting 3 from the first day and exit, and so on. Calculate the number of people exiting each round and the number of the remaining persons.

Source: Internet
Author: User

Title: 13 people in a circle, report the number from 1 to 3, and exit, and so on, calculate the number of the person who exits each round, and the number of the remaining person.

 

Node object:

Public class ListNode
{

Private int postion;
Private ListNode nextNode;
Private bool isOut;

/// <Summary>
/// Location
/// </Summary>
Public int Postion {get {return postion;} set {postion = value ;}}

/// <Summary>
/// Address of the next list
/// </Summary>
Public ListNode NextNode {get {return nextNode;} set {nextNode = value ;}}

/// <Summary>
/// Determine whether to exit
/// </Summary>
Public bool IsOut {get {return isOut;} set {isOut = value ;}}

/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "nextNode"> address of the next node </param>
/// <Param name = "postion"> position in the list </param>
Public ListNode (ListNode nextNode, int postion)
{
This. postion = postion;
This. nextNode = nextNode;
}

Public ListNode ()
{}
}

Loop list class:

/// <Summary>
/// Loop list
/// </Summary>
Public class PeopleLoopList
{
Public ListNode headList;
Private int length;

/// <Summary>
/// List Length
/// </Summary>
Public int Length {get {return length ;}}
/// <Summary>
/// Initialize the list capacity
/// </Summary>
/// <Param name = "amount"> List capacity </param>
Public PeopleLoopList ()
{
Length = 0;
HeadList = new ListNode (null, 0 );
HeadList. NextNode = headList;
}

/// <Summary>
/// Add an item
/// </Summary>
/// <Param name = "node"> </param>
Public void AppendList (ListNode node)
{
If (node = null)
{
Console. WriteLine ("node cannot be blank ");
Return;
}

ListNode tempNode = headList;
/// Add
While (tempNode. NextNode! = HeadList)
{

TempNode = tempNode. NextNode;
}
TempNode. NextNode = node;
//// Loop list
Node. NextNode = headList;
Length ++;
}



/// <Summary>
/// Location of the removed object
/// </Summary>
/// <Param name = "I"> location </param>
Public void RemoveList (int I)
{
ListNode p = headList;
ListNode q = new ListNode ();
If (I = 1)
{
HeadList. NextNode = p. NextNode. NextNode;
Length --;
Return;
}

While (p. NextNode! = HeadList)
{
Q = p;
P = p. NextNode;
If (q. NextNode. Postion = I)
{
Q. NextNode = p. NextNode;
Length --;
Return;
}
}

Console. WriteLine ("no data found ");

}

/// <Summary>
/// Obtain the depth
/// </Summary>
/// <Returns> </returns>
Public int GetLength ()
{
Int length = 0;
ListNode p = headList;
ListNode q = new ListNode ();
While (p. NextNode! = HeadList)
{
Q = p. NextNode;
P = q;
Length ++;
}
Return length;
}

/// <Summary>
/// Output Array
/// </Summary>
Public void OutPutCalled3Result ()
{
ListNode p = headList. NextNode;
ListNode q = new ListNode ();
List <int> tempOut = new List <int> ();
Int callNum = 1;
Int I = 1;
While (this. length> 1)
{

If (callNum = 3)
{
TempOut. Add (p. Postion );
CallNum = 1;
}
Else
{
CallNum ++;
}

If (p. NextNode = headList)
{
Console. Write ("exit round {0}", I );
Foreach (var item in tempOut)
{
This. RemoveList (item );
Console. Write ("{0}", item );
}
Console. WriteLine ();
This. OutPutResult ();
Console. WriteLine ();
TempOut. Clear ();
I ++;
P = p. NextNode. NextNode;
}
Else
{
P = p. NextNode;
}
}
}

Public void OutPutResult ()
{
Console. WriteLine ("existing list :");
ListNode p = headList;
While (p. NextNode! = HeadList)
{
P = p. NextNode;
Console. Write ("{0}", p. Postion );
}
}

}

Method call: PeopleLoopList tempList = new PeopleLoopList ();
InitPeopleLoopList (tempList, amount );
TempList. OutPutCalled3Result ();

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.