请问各位高手,如何使数组中相同的元素不相邻,急求解决方案,在线等!

解决方案 »

  1.   

    关键就是找到后如何把相同并相邻的元素隔开,有没有高手能提供代码???
    例如数组中元素如下:
    a
    a
    a
    c
    c
    b
    e
    e
    e
    e
    e
    g
    g
    f
    f
    f
    如何重新生成数组,其中元素还是这些,只是改变其顺序,例如a和a不相邻……
      

  2.   

    感觉比较麻烦
    数据结构我的水平更差
    能想到的就是比对
    例如数组A A[0]<==>A[1]比较,不同放入另一个数组
    相同A[1]<===>A[2],不同A[2]直接放入另一个数组
    相同A[2]<==>A[3]比较,不同,将A[3]放入另一个数组A[2]的位置
    这样比较烦
    呵呵
      

  3.   

    感觉是不是规则还不够完善,如果你只是想要一个数组中相邻的两个元素不同的话,不考虑其它的顺序等等,那实现起来是比较容易的。先把这个数组排下序,升序降序无所谓,这样处理起来会简单很多,具体方法如下。/// <summary>
    /// 将有序的整形数组,进行相邻位都不相等的处理
    /// </summary>
    /// <param name="intArr"></param>
    /// <returns></returns>
    public int[] ArrayHandle(int[] intArr)
    {
        int[] result = new int[intArr.Length];
        int intMiddle = intArr.Length / 2;
        if (intArr[0] == intArr[intMiddle])
        {
            throw new Exception("数组中有相同的元素过半,无法转换!");
        }
        //将排过序的数组一次按上半部分元素,下半部分元素依次往新数组里面赋值
        for (int i = 0, j = 0; i < intMiddle; i++)
        {
            result[j] = intArr[i];
            j++;
            result[j] = intArr[intMiddle + 1];
        }
        if (intArr.Length % 2 != 0)
        {
            result[result.Length - 1] = intArr[result.Length - 1];    }
        return result;
    }
      

  4.   

    用LINQ做的            string[] str = { "a", "a", "a", "c", "c", "b", "e", "e", "e", "e", "e", "g", "g", "f", "f", "f" };            var Query = str.Select((x, y) => new { x = x, y = y ,num=str.Where(t=>t==x).Select(t=>t).Count()});            foreach (var v in Query.OrderBy(x=>x.y%Query.Max(s=>s.num)))
                {
                    Console.WriteLine(v.x);
                }
                /*
                a
                b
                e
                f
                a
                e
                g
                a
                e
                g
                c
                e
                f
                c
                e
                f
                */
      

  5.   


                string[] str = { "a", "a", "a", "c", "c", "b", "e", "e", "e", "e", "e", "g", "g", "f", "f", "f" };            var Query = str.Select((x, y) => new { x = x, y = y ,num=str.Where(t=>t==x).Select(t=>t).Count()});            foreach (var v in Query.OrderBy(x=>x.y%Query.Max(s=>s.num)))
                {
                    Console.WriteLine(v.x+"    "+v.y%Query.Max(s=>s.num));
                }            /*
                a    0
                b    0
                e    0
                f    0
                a    1
                e    1
                g    1
                a    2
                e    2
                g    2
                c    3
                e    3
                f    3
                c    4
                e    4
                f    4            */            Console.ReadKey();
      

  6.   

    (不逆) 的Linq运用的太棒了。
      

  7.   

    to 不逆 。你看我来回帖!你才来的!那你要我来看撒呢!Linq吗?呵呵!
      

  8.   

    用快速排序
     public class Sort
        {
            public Sort()
            {        }
            private void Swap(ref int i, ref int j)
            //swap two integer 
            {
                int t;
                t = i;
                i = j;
                j = t;
            }        public void QiuckSort(int[] list, int low, int high)
            {
                if (high <= low)
                {
                    //only one element in array list 
                    //so it do not need sort 
                    return;
                }
                else if (high == low + 1)
                {
                    //means two elements in array list 
                    //so we just compare them 
                    if (list[low] > list[high])
                    {
                        //exchange them 
                        Swap(ref list[low], ref list[high]);
                        return;
                    }
                }
                //more than 3 elements in the arrary list 
                //begin QuickSort 
                myQuickSort(list, low, high);
            }        public void myQuickSort(int[] list, int low, int high)
            {
                if (low < high)
                {
                    int pivot = Partition(list, low, high);
                    myQuickSort(list, low, pivot - 1);
                    myQuickSort(list, pivot + 1, high);
                }
            }        private int Partition(int[] list, int low, int high)
            {
                //get the pivot of the arrary list 
                int pivot;
                pivot = list[low];
                while (low < high)
                {
                    while (low < high && list[high] >= pivot)
                    {
                        high--;
                    }
                    if (low != high)
                    {
                        Swap(ref list[low], ref list[high]);
                        low++;
                    }
                    while (low < high && list[low] <= pivot)
                    {
                        low++;
                    }
                    if (low != high)
                    {
                        Swap(ref list[low], ref list[high]);
                        high--;
                    }
                }
                return low;
            } 
    }
    就可以看出来了
      

  9.   

    /// <returns></returns>
    public int[] ArrayHandle(int[] intArr)
    {
        int[] result = new int[intArr.Length];
        int intMiddle = intArr.Length / 2;
        if (intArr[0] == intArr[intMiddle])
        {
            throw new Exception("数组中有相同的元素过半,无法转换!");
        }
        //将排过序的数组一次按上半部分元素,下半部分元素依次往新数组里面赋值
        for (int i = 0, j = 0; i < intMiddle; i++)
        {
            result[j] = intArr[i];
            j++;
            result[j] = intArr[intMiddle + 1];
        }
        if (intArr.Length % 2 != 0)
        {
            result[result.Length - 1] = intArr[result.Length - 1];    }
        return result;
    }