数据结构好的兄弟们进来,求教一个首领的算法

yudi010 2007-03-09 01:24:29
假设我拥有一个数组,长度为30,然后每一个数组元素里面都有一个唯一的值
然后从头开始,数到六的时候,就把这个数值淘汰,反复循环,直到剩下最后一个为止
请问谁能够把这个算法实现阿
多谢了
...全文
324 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yudi010 2007-03-14
  • 打赏
  • 举报
回复
强手真多
我想了半天也没有得出好的解法
谢谢大家
sweetbai 2007-03-12
  • 打赏
  • 举报
回复
class MyClass
{
static void Main(string[] args)
{
int index = 0;
MyClass pg = new MyClass();
ArrayList al = new ArrayList(30);
for (int i = 0; i < 30; i++)
{
al.Add(i);
}

for (; ; index++)
{
if (al.Count <= 1)
{
break;
}
else if ((index + 1) % 6 == 0)
{
al.RemoveAt(index%al.Count);
pg.Display(al);
}

}
pg.Display(al);//其值即为原ArrayList的索引。

}

public void Display(ArrayList al)
{
foreach (int i in al)
{
Console.Write(i.ToString() + " ");
}
Console.WriteLine();
}

}
果然 int index = 4;
rononwang 2007-03-11
  • 打赏
  • 举报
回复
貌似用二维数组搞定
Dabay 2007-03-11
  • 打赏
  • 举报
回复
using System;
using System.Collections;

public class MyClass
{
public static void Main()
{
int total=30;
int step =6;
ArrayList arr = new ArrayList();
for(int i=0;i<total;i++)
{
arr.Add(i+1);
}
int index=0;
while(arr.Count>1)
{
for(int t=0;t<step-1;t++)
{
index+=1;
if(index>=arr.Count)
{
index=0;
}
}
arr.RemoveAt(index);
if(index>=arr.Count)
index=0;
}
Console.WriteLine(arr[0].ToString());
Console.ReadLine();
}
}
刚才错了,不好意思,答案就是syeerzy(快乐永远*先天下之乐而乐*后天下之忧而忧*) 所说的4
Dabay 2007-03-11
  • 打赏
  • 举报
回复
using System;
using System.Collections;

public class MyClass
{
public static void Main()
{
ArrayList arr = new ArrayList();
for(int i=0;i<30;i++)
{
arr.Add(i);
}
int index=0;
int step =6;
while(arr.Count>1)
{
for(int t=0;t<step;t++)
{
IndexIncrease(ref index,arr);
}
arr.RemoveAt(index);
IndexIncrease(ref index,arr);
}
WL(arr[0].ToString());
RL();
}

#region Helper methods

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}

private static void RL()
{
Console.ReadLine();
}

private static void Break()
{
System.Diagnostics.Debugger.Break();
}

#endregion


static void IndexIncrease(ref int index,ArrayList arr)
{
index+=1;
if(index>=arr.Count)
{
index=0;
}
}

}

---------------------
答案是29
(如果我的解法没有错的话)
Dabay 2007-03-11
  • 打赏
  • 举报
回复
ArrayList arr = new ArrayList();
for(int i=0;i<30;i++)
{
arr.Add(i);
}
int index=0;
int step =6;
while(arr.Count>1)
{
for(t=0;t<step;t++)
{
IndexIncream(index);
}
arr.RemoveAt(index);
IndexIncream(index);
}
return arr[0];

------------------
void IndexIncream(ref int index)
{
index+=1;
if(index>=arr.Count)
{
index=0;
}
}
syeerzy 2007-03-09
  • 打赏
  • 举报
回复
长度固定30,步长固定6 ,没什么好算法不算法的。


int i = 4;
arjsyy 2007-03-09
  • 打赏
  • 举报
回复
用单循环链表就可以实现
edsoft 2007-03-09
  • 打赏
  • 举报
回复
最后5个怎么删除?这种问题干吗的?
smartcreater 2007-03-09
  • 打赏
  • 举报
回复
在计算机中数组是线性结构,
你重复从头开始数[1..6], 肯定会剩下数组最后的5个元素。

搂住的题意数据不是放在数组中,一般是在链表结果中才会剩下一个
cancerser 2007-03-09
  • 打赏
  • 举报
回复
ArrayList al=new ArrayList();
int i,index=0;
for(i=0;i<30;i++) al.Add(i);
i=0;
while(al.Count!=1)
{
i++;
if(al.Count==index) index=0;
if(i%6==0)
{
al.RemoveAt(index);
i=0;
index--;
}
index++;
}//第一印象写出来的
loverain9999 2007-03-09
  • 打赏
  • 举报
回复
是Josephus问题吧~~刚看了数据结构 C语言用单连表解决的
Red_angelX 2007-03-09
  • 打赏
  • 举报
回复
是不是这个意思 每6个除掉一个 一直循环到只剩下1个?
循环炼表去搞吧
cangwu_lee 2007-03-09
  • 打赏
  • 举报
回复
这句话矛盾,究竟是一个数组?还是一批数组?
--------------
假设我拥 有一个数组,长度为30,然后 每一个数组 元素里面都有一个唯一的值

111,119

社区成员

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

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

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