日期:2014-05-20 浏览次数:21201 次
static void Main(string[] args)
{
int[] a = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
OutQueue(a, 2, 4);
}
static void OutQueue(int[] obj, int k, int m)
{
if (obj == null || obj.Length == 0)
return;
if (k < 1 || k > obj.Length)
{
Console.WriteLine("K value is invalid!");
return;
}
if (m <= 0)
{
Console.WriteLine("m value is invalid!");
return;
}
int count = 0; //同m比较,数到m就输出当前位置
int mod = m % obj.Length == 0 ? obj.Length : m % obj.Length; //防止m超过数组长度,取模代替之
int index = k-1; //存放当前的index,为什么要-1?因为数组下标从0开始
int got = 0;
while (got < obj.Length - 1)
{
count = 1;
for (int j = 0; j < mod; j++)
{
if (count == mod)
{
while (obj[index % obj.Length] == 0)
index++;
Console.WriteLine("The {0} person is out of queue!", index % obj.Length + 1);
got++;
obj[index % obj.Length] = 0;
}
count++;
index++;
}
}
for (int i = 0; i < obj.Length; i++)
{
if (obj[index % obj.Length] != 0)
Console.WriteLine("The {0} person is out of queue!", index % obj.Length + 1);
index++;
}
}
The 5 person is out of queue! The 9 person is out of queue! The 2 person is out of queue! The 6 person is out of queue! The 10 person is out of queue! The 3 person is out of queue! The 7 person is out of queue! The 11 person is out of queue! The 4 person is out of queue! The 8 person is out of queue! The 1 person is out of queue!
using System;
using System.Collections.Generic;
using System.Text;
namespace ExMonkey
{
class Monkey
{
public int King(int M, int N)
{
//总人数 M ,数到第 N 个排除。
int k=0;
for (int i = 2; i <= M; i++)
k = (k + N) % i;
return ++k;
}
static void Main(string[] args)
{
Monkey M = new Monkey();
Console.WriteLine ("第"+M.King(10,3)+"号猴子为大王。");
}
}
}
------解决方案--------------------
只要排成队的增插删改一律用链表,高效。
------解决方案--------------------
using System;