Advertisement
J00ker

(10-14)E

Oct 14th, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int n = 10, a[10];
  8.  
  9. void Generare(int a[], int n)
  10. {
  11. srand(time(0));
  12. for(int i = 0; i < n; i++)
  13. a[i] = rand() % 10 + 1;
  14. }
  15.  
  16. void Afisare(int a[], int n)
  17. {
  18. for(int i = 0; i < n; i++)
  19. cout << a[i] << " ";
  20. cout << "\n\n";
  21. }
  22.  
  23. int InterschParitate(int a[], int n )
  24. {
  25. /*int i, j;
  26. for(i = 0, j = n-1; i < j;)
  27. {
  28. if((a[i] % 2 == 1)&&(a[j] % 2 == 0))
  29. {
  30. int aux = a[i];
  31. a[i] = a[j];
  32. a[j] = aux;
  33. j--;
  34. }
  35. else if((a[i] % 2 == 1)&&(a[j] % 2 == 1)) j--;
  36. else i++;
  37. }
  38. return i+1;
  39. */
  40. int i = 0, j = n-1;
  41. while(i < j)
  42. {
  43. if(a[i] % 2 == 1) j--;
  44. if(a[i] % 2 == 0) i++;
  45. if(i < j && a[i] % 2 == 1 && a[j] % 2 == 0)
  46. {
  47. int aux = a[i];
  48. a[i] = a[j];
  49. a[j] = aux;
  50. i++;j--;
  51. }
  52. }
  53. return i;
  54. }
  55.  
  56. int main()
  57. {
  58. Generare(a, n);
  59. Afisare(a, n);
  60. cout << InterschParitate(a, n) << "\n\n";
  61. Afisare(a, n);
  62.  
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement