由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)数组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);
//
}
}程序写了一部分
解决方案 »
- 求一个C#定时FTP多线程下载文件的源码多谢了
- WEB服务器ASP.NET网页怎么和WEB服务器上运行的聊天服务器(TCP/IP)程序通信呢?
- 怎样将 DateTime 类型时间转换为24小时制,而不是12小时制的?
- Win32Api :: GetPrivateProfileSection 濡備綍浣跨敤
- webBrowser
- C#.net开发的程序可以在wince.net下直接运行吗?
- 像QQ和MSN这样的软件的服务段是怎么做的?
- 再来送点分!谁在W98下能回复帖子的?要装什么?我都按公告里的装了,就是不行。TMD XML!
- IE11的F12功能无法正常使用
- 大家好,我是一位菜鸟。
- C#动态在Form里面画圆
- xml作为配置文件的好处
应该是表述不清楚 有2个大小相同的数组,由此2个数组的部分元素组成一个新的数组
要求满足条件 如图:
(1)在父代1中选择n个不重复的位置,并将这些位置上的整数值赋值给子代此相同位置
(2)子代余下的位置的数值通过父代2进行从左到右的扫描,并填充空位得到方法如lz的图
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;
}
}
}
}
{
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);
}
}