Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<stdlib.h>
- #include<time.h>
- using namespace std;
- int gena[100];// = { 1,2,3,4,5,6,7,8,9,10 };
- int n;
- int main() {
- srand(time(NULL));
- cout << "n= ";
- cin >> n;
- int newnr, pos = 0,ok=0;
- //generare n numere aleatoare diferite
- while(pos<n)
- {
- newnr = rand() % 20;
- ok = true;
- for (int j = 0;j < pos;j++) {
- if (newnr == gena[j]) {
- ok = false;
- }
- }
- if (ok == true) {
- gena[pos++] = newnr;
- }
- }
- cout << "cromozom initial" << endl;
- for (int i = 0;i < n;i++)
- cout << gena[i] << " ";
- cout << endl;
- //generare pozitii inversiune
- int poz1 = rand() % n;
- int poz2 = rand() % n;
- while(poz2 < poz1) {
- int aux = poz1;
- poz1 = poz2;
- poz2 = aux;
- }
- cout << endl << "poz1= " << poz1 << endl << "poz2= " << poz2<<endl;
- poz1--;
- poz2--;
- //int n = 10;
- //creare inversiune
- int i,aux,k=0;
- for (i = 0;i < n;i++) {
- if (i == poz1) {
- for (int j = i;j <= (poz2+j)/2;j++) {
- aux = gena[j];
- gena[j] = gena[poz2-k];
- gena[poz2 - k] = aux;
- k++;
- }
- i += poz2 - poz1;
- }
- }
- cout << "cromozom final" << endl;
- for (int i = 0;i < n;i++)
- cout << gena[i] << " ";
- cout << endl;
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement