日期:2014-05-20  浏览次数:20911 次

数据结构好的兄弟们进来,求教一个首领的算法
假设我拥有一个数组,长度为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++)