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 ();