fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. void cau3(int a[], int n) {
  7. int i = 0, j = 0;
  8. int val = 1;
  9. while (i < n && j < n) {
  10. if (!(val == 1 && a[i] > 0) || !(val == -1 && a[i] < 0)) {
  11. j = i;
  12. while (j < n && val*a[j] < 0) j++;
  13. if (j == n) break;
  14. swap(a[j], a[i]);
  15. }
  16. i++;
  17. val *= -1;
  18. }
  19. }
  20. int main() {
  21. int a[] = {8, -8, -8, -8, -8, -8, 8};
  22. int n = sizeof(a) / sizeof(a[0]);
  23.  
  24. cau3(a, n);
  25.  
  26. // Output the result
  27. for (int i = 0; i < n; i++) {
  28. printf("%d ", a[i]);
  29. }
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
8 -8 8 -8 -8 -8 -8