[抛砖引玉]一起来做道算法题吧。

zhengqian529 2010-09-01 04:59:02
加精
有n个孩子(编号1,2...n)围成一圈,现在从编号为k的孩子开始报数,当报数到m时,报m的那个孩子出列,然后从报m的那个孩子的下一个孩子重新开始从1报数...
求:孩子们出列的序列。

以上是题目,我自己实现了一下,感觉不好,但测试了几个,序列还是正确的,现贴出来,希望和大家讨论以后能够得到更精妙的算法。本人算法实在是太差了。


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!
...全文
4541 258 打赏 收藏 转发到动态 举报
写回复
用AI写文章
258 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jor_Key 2010-09-12
  • 打赏
  • 举报
回复

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

测试序列:{1,2,3,4,5,6,7,8,9,0}
条件:报数到3则取出,接着继续从1开始
结果:
3
6
9
2
7
1
8
5
0
4
siril 2010-09-10
  • 打赏
  • 举报
回复
这类算法有用处吗?
PapaDog_ 2010-09-10
  • 打赏
  • 举报
回复
这明明就是教材上的例子!
zhaojianqiang19 2010-09-10
  • 打赏
  • 举报
回复
java代码
/**
*
* @param n
* 孩子总数
* @param k
* 从编号为k的孩子开始报数
* @param m
* 报数到m时,报m的那个孩子出列
*/
public static void out(int n, int k, int m) {
ArrayList value = new ArrayList();
int count = 0;
boolean tag = false;
for (int i = 0; i < n; i++) {
value.add((i + 1) + "");
}
int s = k - 1;
if (n < k) {
System.out.println("孩子数小于编号数,输入有误!");
} else {
for (; s < value.size();) {
tag = false;
count++;
if (count == m) {
tag = true;
count = 0;
System.out.println("number " + value.get(s) + " 孩子出列");
value.remove(s);
if (s >= value.size()) {
s = 0;
}
}
if (!tag && (s <= value.size() - 1)) {
s++;
}
if (!tag && (s >= (value.size()))) {
s = 0;
}
}
}
}
心如刀割 2010-09-10
  • 打赏
  • 举报
回复

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)+"号猴子为大王。");
}


这个是最简单的,但是很不理解啊,哪位高手解释下?
zhaojianqiang19 2010-09-10
  • 打赏
  • 举报
回复
java代码:
/**
*
* @param n
* 孩子总数
* @param k
* 从编号为k的孩子开始报数
* @param m
* 报数到m时,报m的那个孩子出列
*/
public static void out(int n, int k, int m) {
ArrayList value = new ArrayList();
int count = 0;
boolean tag = false;
for (int i = 0; i < n; i++) {
value.add((i + 1) + "");
}
int s = k - 1;
if (n < k) {
System.out.println("孩子数小于编号数,输入有误!");
} else {
for (; s < value.size();) {
tag = false;
count++;
if (count == m) {
tag = true;
count = 0;
System.out.println("number " + value.get(s) + " 孩子出列");
value.remove(s);
if (s >= value.size()) {
s = 0;
}
}
if (!tag && (s <= value.size() - 1)) {
s++;
}
if (!tag && (s >= (value.size()))) {
s = 0;
}
}
}
}
HappyKeKe 2010-09-10
  • 打赏
  • 举报
回复
数学不好 看的头晕...
zhaojianqiang19 2010-09-10
  • 打赏
  • 举报
回复
[Quote=引用 252 楼 xiaobadi 的回复:]
Java code

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)
{
……这是一个简单的数学问题
[/Quote]
向往悠然 2010-09-09
  • 打赏
  • 举报
回复
学习了
哎!时间不够用啊
king138888 2010-09-09
  • 打赏
  • 举报
回复
学习一下。。。。。
k3109852 2010-09-09
  • 打赏
  • 举报
回复

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

}
h280338871 2010-09-09
  • 打赏
  • 举报
回复
你这个明显错了 最后一个孩子很显然是不可能出来的 除非他们全部报1
Davy 2010-09-08
  • 打赏
  • 举报
回复
打酱油的路过
huanjun1106 2010-09-08
  • 打赏
  • 举报
回复
很好 不错
心如刀割 2010-09-07
  • 打赏
  • 举报
回复
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)+"号猴子为大王。");
}
百思不得其解,哪位高人解释下?
yanxueyan 2010-09-06
  • 打赏
  • 举报
回复
好厉害啊 ,够逻辑
rwaco 2010-09-06
  • 打赏
  • 举报
回复
谢谢,哈
aeayo 2010-09-06
  • 打赏
  • 举报
回复
学习有用
nikigeng 2010-09-06
  • 打赏
  • 举报
回复
学习一下谢谢!
xiaotanyu13 2010-09-06
  • 打赏
  • 举报
回复
呵呵 第一次 ACM竞赛 时 有这道题目
加载更多回复(213)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧