数据结构好的兄弟们进来,求教一个首领的算法
假设我拥有一个数组,长度为30,然后每一个数组元素里面都有一个唯一的值
然后从头开始,数到六的时候,就把这个数值淘汰,反复循环,直到剩下最后一个为止
请问谁能够把这个算法实现阿
多谢了
[解决办法]
这句话矛盾,究竟是一个数组?还是一批数组?
--------------
假设我拥 有一个数组,长度为30,然后 每一个数组 元素里面都有一个唯一的值
[解决办法]
是不是这个意思 每6个除掉一个 一直循环到只剩下1个?
循环炼表去搞吧
[解决办法]
是Josephus问题吧~~刚看了数据结构 C语言用单连表解决的
[解决办法]
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++;
}//第一印象写出来的
[解决办法]
在计算机中数组是线性结构,
你重复从头开始数[1..6], 肯定会剩下数组最后的5个元素。
搂住的题意数据不是放在数组中,一般是在链表结果中才会剩下一个
[解决办法]
最后5个怎么删除?这种问题干吗的?
[解决办法]
用单循环链表就可以实现
[解决办法]
长度固定30,步长固定6 ,没什么好算法不算法的。
int i = 4;
[解决办法]
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;
}
}
[解决办法]
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
(如果我的解法没有错的话)
[解决办法]
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
[解决办法]
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;