110,534
社区成员
发帖
与我相关
我的任务
分享
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!
protected void JosephProblem(int[] m, int n) {
LinkedList ll = new LinkedList();
int j = 1;
int t = 0;
for(int i = 0; i < m.length; i++) {
ll.add(m[i]);
}
do {
if(j % n == 0) {
System.out.println(ll.remove().toString());
t = t + 1;
} else {
ll.add(ll.remove());
}
j++;
}while(t != m.length);
}
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;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> children = new List<string>();
for (int i = 0; i < 11; i++)
{
children.Add((i + 1).ToString());
}
try
{
Sequence(children, 2, 4);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
static void Sequence(List<string> children, int first, int count)
{
int num = (count + (first - 1)) % children.Count;
if (num == 0)
num = children.Count;
Console.WriteLine(children[num - 1]);
children.RemoveAt(num - 1);
if (children.Count != 0)
Sequence(children, num, count);
}
}
}