When I learned c a long time ago, I got out of the game (Joseph Ring), but I was interested in only two days.
Today I saw a Java also has a similar problem, feel the use of object-oriented to do more appropriate ...
Title: 30 people stand in a circle, starting with the first person, every 9 people out of one, a total of 15 out of the list, and the number of people who should be out of the
Here is the C # version of the solution.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text; Namespace ConsoleApplication4 {class Program {static void Main (string[] args) {//Total 3
0 Persons int count = 30;
Need to exclude 15 persons int remainingcount = 15;
Every number 9 people out of 1 people int position = 9;
Constructing ring chain list<person> List = new list<person> ();
for (int i = 1; I <= count, i++) {person p = new person () { Id = i, previd = i = = 1? COUNT:I-1, NextID = i = = count?
1:i + 1, out = false}; List.
ADD (P); //Loop until you find all the people who need to be excluded,/note: In order for the first number to be 1, the first one out is 9, the initial position is the last person (currid=30) for (int j = 0 , Currid = 30; J < Remainingcount; J + +) {person Currperson = list.
Find (p => p.id = = Currid);
int thenextid = 0;
for (int k = 0; k < position; k++) {thenextid = Currperson.nextid; Currperson = list.
Find (p => p.id = = Thenextid);
} Currperson.out = true;
Console.WriteLine ("Out of number {0}", currperson.id); Person prev = list.
Find (p => p.id = = Currperson.previd); Prev.
NextID = Currperson.nextid; Person next = list.
Find (p => p.id = = Currperson.nextid); Next.
Previd = Currperson.previd;
Currid = currperson.id;
} console.readline ();
public class Person {public int Id {get; set;}
public int Previd {get; set;}
public int NextID {get; set;}
public bool out {get; set;} }
}
No object-oriented solution:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text; Namespace ConsoleApplication5 {class Program {static void Main (string[] args) {cons t int count = 30; Total number of people const int outcount = 15; How many people need to be voted out? const int position = 9; Each number to the first few people out int currid =-1; ID of the current number to, because you want to start counting from subscript 0 bool[] arr = new bool[count];//all elements default to False, just in case there is no exit for (int i = 1; ; = Outcount; i++) {for (int j = 1; j <= position; j) {if (++c
urrid>count-1) {Currid = 0;
} if (Arr[currid]) j--;
} Arr[currid] = true;
Console.WriteLine ("{0}: numbered {1}", I, currid+1);
} console.read ();
} }
}