Advertisement
STANAANDREY

mv neg to start myver

Oct 7th, 2023
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #define NMAX 101
  5.  
  6. void swap(int* a, int* b) {
  7.     int tmp = *a;
  8.     *a = *b;
  9.     *b = tmp;
  10. }
  11.  
  12. void moveNegsToStart(int n, int arr[]) {
  13.     int j = 0;
  14.     while (arr[j] < 0) {
  15.         j++;
  16.     }
  17.     for (int i = j + 1; i < n; i++) {
  18.         if (arr[i] < 0) {
  19.             swap(&arr[i], &arr[j]);
  20.             j++;
  21.             while (arr[j] < 0) {
  22.                 j++;
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28. int main(void) {
  29.     int n;
  30.     assert(scanf_s("%d", &n));
  31.     static int arr[NMAX];
  32.     for (int i = 0; i < n; i++) {
  33.         assert(scanf_s("%d", &arr[i]));
  34.     }
  35.     moveNegsToStart(n, arr);
  36.     for (int i = 0; i < n; i++) {
  37.         printf("%d ", arr[i]);
  38.     }
  39.     putchar('\n');
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement