假设我拥有一个数组,长度为30,然后每一个数组元素里面都有一个唯一的值
然后从头开始,数到六的时候,就把这个数值淘汰,反复循环,直到剩下最后一个为止
请问谁能够把这个算法实现阿
多谢了

解决方案 »

  1.   

    是Josephus问题吧~~刚看了数据结构 C语言用单连表解决的
      

  2.   

    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++;
    }//第一印象写出来的
      

  3.   

    长度固定30,步长固定6  ,没什么好算法不算法的。
    int i = 4;
      

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

  5.   

    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
    (如果我的解法没有错的话)
      

  6.   

    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
      

  7.   

    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;