由2个整形数组大小一样 
(1)数组A随机取得n个位置的元素,并赋值给相同位置的的新生成数组
(2)新生成的数组的其他元素如数组B的相同位置的数值写了部分代码 请高手指教
 public int[] pb_crossover(int[]parentA,int[] parentB,int positionNum)
        {
            Random ran = new Random();
            ArrayList al = new ArrayList();            for (int j = 0; j < parentA.Length; j++)
            {
                al.Add(parentA[j]);
            }
            for (int i = 0; i < positionNum; i++)
            {
                int r = ran.Next(positionNum - 1-i);
                al.RemoveAt(r);
                //
            }
        }程序写了一部分 

解决方案 »

  1.   

    对不起 楼上的 这个是我自己的问题
    应该是表述不清楚 有2个大小相同的数组,由此2个数组的部分元素组成一个新的数组
    要求满足条件 如图:
    (1)在父代1中选择n个不重复的位置,并将这些位置上的整数值赋值给子代此相同位置
    (2)子代余下的位置的数值通过父代2进行从左到右的扫描,并填充空位得到方法如lz的图 
      

  2.   

    1. 生成随机位置数组,比如你上面生成的数组是 A{1,4,5} //巧合...刚好位置是和值一样...刚好是第2,5,6个数字,所以索引145..
    2. 根据上面的A{1,4,5}去PA里面取数字再组成值数组..还是B{1,4,5}
    3. 创建一个和PA一样长的C数组
    4. 
        for(int i = 0; i < C.Length; i++ ){
           if(Array.IndexOf<int>(A,i)>=0){
               C[i] = PA[i];
           }
           else{
               for(int j = 0;j<PB.Length;j++){
                   if(Array.IndexOf<int>(B,PB[j]) < 0 && Array.IndexOf<int>(C,PB[j]) < 0 ){
                       C[i] = PB[j];
                       break;
                   }
               }
           }
       }
      

  3.   

    using System;class pb
    {
        Random r;
        public pb()
        {
            r = new Random();
        }    public int[] pb_crossover(int[] parentA, int[] parentB, int positionNum)
        {
            // 存放结果的数组,0代表未赋值的空元素
            int[] child = new int[parentA.Length];  // 初始化时元素值全部是 0
            // 从左往右扫描parentB时,是否跳过该位置的数字,true表示跳过
            bool[] overParentB = new bool[parentB.Length];  // 初始化时元素值全部是 false        // 生成n个随机位置,用parentA里该位置的数字填充child,并在parentB里标出已被使用过的数字。
            for (int p, i = 0; i < positionNum; i++)
            {
                // 在找到重复的p时,执行空循环,否则跳出循环,用parentA里对应的数字填充child
                while (child[p = r.Next(parentA.Length)] != 0) ;            child[p] = parentA[p];
                overParentB[Array.IndexOf<int>(parentB, parentA[p])] = true;
            }        // 从左往右扫描parentB,填充child的空元素。
            for (int p = -1, i = 0; i < child.Length; i++)
                if (child[i] == 0)
                {
                    // 如果parentB扫描到的数字需要跳过,执行空循环,否则跳出循环,用ParentB里找到的数字填充child。
                    while (overParentB[++p]) ;
                    child[i] = parentB[p];
                }        return child;
        }
    }class Program
    {
        static void Main(string[] args)
        {
            int n = 3;
            int[] parentA ={ 3, 1, 7, 6, 4, 5, 2 };
            int[] parentB ={ 6, 5, 7, 1, 4, 2, 3 };
            int[] child = new pb().pb_crossover(parentA, parentB, n);
        }
    }