c++遗传算法的编码解解码问题
发布网友
发布时间:2022-06-06 03:25
我来回答
共1个回答
热心网友
时间:2024-11-22 19:37
#include<iostream>
using namespace std;
float transVaul(char *buf);
int main()
{
char buf[21] = "";
float arr[20] = { 0 };
cout<<"输入20位基因编码:\n";
//比如11110000111100001111
//输入很粗糙,只是为了演示程序执行
for (int i = 0; i < 20; i++)
{
cout << i + 1 << ":\t";
memset(buf,0,21);
cin >> buf;
arr[i] = transVaul(buf);
}
cout << "对应的十进制是:\n";
for (int j = 0; j < 20; j++)
printf("%f\n",arr[j]);
return 0;
}
float transVaul(char *buf)
{
float score = 0;
unsigned *pt = (unsigned *)(&score);
for (int i = 0; i < 20; i++)
{
*pt|=(buf[i] - 48);
*pt <<= 1;
}
*pt=*pt <<2|0x3f800000;
return score-1;
}