Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5. using namespace std;
  6.  
  7. int gena[100];// = { 1,2,3,4,5,6,7,8,9,10 };
  8. int n;
  9.  
  10. int main() {
  11.  
  12.  
  13.  
  14. srand(time(NULL));
  15.  
  16.  
  17. cout << "n= ";
  18. cin >> n;
  19. int newnr, pos = 0,ok=0;
  20.  
  21. //generare n numere aleatoare diferite
  22.  
  23. while(pos<n)
  24. {
  25. newnr = rand() % 20;
  26. ok = true;
  27. for (int j = 0;j < pos;j++) {
  28. if (newnr == gena[j]) {
  29. ok = false;
  30. }
  31. }
  32. if (ok == true) {
  33. gena[pos++] = newnr;
  34. }
  35. }
  36. cout << "cromozom initial" << endl;
  37. for (int i = 0;i < n;i++)
  38. cout << gena[i] << " ";
  39. cout << endl;
  40.  
  41. //generare pozitii inversiune
  42. int poz1 = rand() % n;
  43. int poz2 = rand() % n;
  44. while(poz2 < poz1) {
  45. int aux = poz1;
  46. poz1 = poz2;
  47. poz2 = aux;
  48. }
  49. cout << endl << "poz1= " << poz1 << endl << "poz2= " << poz2<<endl;
  50. poz1--;
  51. poz2--;
  52. //int n = 10;
  53. //creare inversiune
  54. int i,aux,k=0;
  55. for (i = 0;i < n;i++) {
  56. if (i == poz1) {
  57. for (int j = i;j <= (poz2+j)/2;j++) {
  58. aux = gena[j];
  59. gena[j] = gena[poz2-k];
  60. gena[poz2 - k] = aux;
  61. k++;
  62.  
  63. }
  64. i += poz2 - poz1;
  65.  
  66. }
  67. }
  68.  
  69. cout << "cromozom final" << endl;
  70. for (int i = 0;i < n;i++)
  71. cout << gena[i] << " ";
  72. cout << endl;
  73.  
  74. _getch();
  75. return 0;
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement