Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int n = 10, a[10];
- void Generare(int a[], int n)
- {
- srand(time(0));
- for(int i = 0; i < n; i++)
- a[i] = rand() % 10 + 1;
- }
- void Afisare(int a[], int n)
- {
- for(int i = 0; i < n; i++)
- cout << a[i] << " ";
- cout << "\n\n";
- }
- int InterschParitate(int a[], int n )
- {
- /*int i, j;
- for(i = 0, j = n-1; i < j;)
- {
- if((a[i] % 2 == 1)&&(a[j] % 2 == 0))
- {
- int aux = a[i];
- a[i] = a[j];
- a[j] = aux;
- j--;
- }
- else if((a[i] % 2 == 1)&&(a[j] % 2 == 1)) j--;
- else i++;
- }
- return i+1;
- */
- int i = 0, j = n-1;
- while(i < j)
- {
- if(a[i] % 2 == 1) j--;
- if(a[i] % 2 == 0) i++;
- if(i < j && a[i] % 2 == 1 && a[j] % 2 == 0)
- {
- int aux = a[i];
- a[i] = a[j];
- a[j] = aux;
- i++;j--;
- }
- }
- return i;
- }
- int main()
- {
- Generare(a, n);
- Afisare(a, n);
- cout << InterschParitate(a, n) << "\n\n";
- Afisare(a, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement