using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
bool[] arr = new bool[500];
for (int i = 0; i < arr.Length; i++)
arr[i] = true;
int index = 0, leftCount = arr.Length, countNum = 0;
while (leftCount > 1)
{
if (arr[index])
{
countNum++;
if (countNum == 3)
{
arr[index] = false;
countNum = 0;
leftCount--;
}
}
index++;
if (index == arr.Length)
index = 0;
}
for (int i = 0; i < arr.Length; i++)
{
if (arr[i])
{
Console.WriteLine(i);
break;
}
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
KidCircle kc = new KidCircle(500);
Console.WriteLine(kc);
Console.ReadKey();
Kid k = kc.first;
int countNum = 0;
while (kc.count > 1)
{
countNum++;
if (countNum % 3 == 0)
{
//怎样表达当前对象
kc.Delete(k);
countNum = 0;
}
k = k.right;
}
Console.WriteLine(kc.first.id);
Console.ReadKey();
}
}
class KidCircle
{
internal int count;
internal Kid first, last;
internal KidCircle(int n)
{
for (int i = 0; i < n; i++)
Add();
}
internal void Add()
{
Kid k = new Kid(); k.id = count;
if (0 == count)
{
first = last = k;
first.left = first.right = k;
}
else
{
last.right = k; k.left = last;
k.right = first; first.left = k;
last = k;
}
count++;
}
internal void Delete(Kid k)
{
//Console.WriteLine(this.count + "/" + k.id + "/left:" + k.left.id + "/right:" + k.right.id);
//k.id不在kc里怎么办?
if (count <= 0)
return;
k.left.right = k.right;
k.right.left = k.left;
count--;
if (k.id == last.id)
{
last = k.left;
}
if (k.id == first.id)
first = k.right;
}
public override string ToString()
{
List<string> ls = new List<string>();
Kid k = first;
for (int i = 0; i < count; i++)
{
ls.Add(string.Format("{0}-Left:{1} Right:{2}", k.id, k.left.id, k.right.id));
k = k.right;
}
return string.Join(Environment.NewLine, ls.ToArray());
}
}
class Kid
{
internal int id;
internal Kid left, right;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Circle(500).FindLast());
Console.ReadKey();
}
}
public class Circle {
private Item[] items;
public Circle(int count) {
this.items = new Item[count];
for (int i = 0; i < count; i++)
{
this.items[i] = new Item(i);
}
}
public int FindLast() {
int result = -1;
int count = 0;
int selectNo = 0;
while (true)
{
if (!this.items[selectNo].Selected) {
count++;
if (count % 3 == 0)
{
items[selectNo].Selected = true;
count = 0;
}
}
selectNo++;
if (selectNo % items.Length == 0)
selectNo = 0;
if (this.TotalNotSelected() == 1)
break;
}
for (int i = 0; i < items.Length; i++)
{
if (!items[i].Selected)
{
result = i;
break;
}
}
return result;
}
public int TotalNotSelected() {
int result = 0;
for (int i = 0; i < this.items.Length; i++)
{
if (!this.items[i].Selected)
{
result++;
}
}
return result;
}
}
public class Item {
public int No { set; get; }
public bool Selected { set; get; }
public Item(int no) {
this.No = no;
}
}
}