Advertisement
STANAANDREY

points on same line

Feb 14th, 2023 (edited)
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Point {
  6.     int x;
  7.     int y;
  8. } Point;
  9.  
  10. void bsort(Point *p, int n) {
  11.   int s = 0;
  12.   do {
  13.     s = 1;
  14.     for (int i = 1; i < n; i++) {
  15.       if (p[i - 1].y > p[i].y) {
  16.         Point aux = p[i];
  17.         p[i] = p[i - 1];
  18.         p[i - 1] = aux;
  19.         s = 0;
  20.       }
  21.     }
  22.   } while(!s);
  23. }
  24.  
  25. int main() {
  26.     int n;
  27.     scanf("%d", &n);
  28.  
  29.     Point *points = (Point *) malloc(n * sizeof(Point));
  30.     for (int i = 0; i < n; i++) {
  31.         scanf("%d %d", &points[i].x, &points[i].y);
  32.     }
  33.  
  34.     bsort(points, n);
  35.     int currentY = points[0].y;
  36.     printf("Puncte pe linia orizontală y = %d: [%d", currentY, points[0].x);
  37.     for (int i = 1; i < n; i++) {
  38.         if (points[i].y != currentY) {
  39.             currentY = points[i].y;
  40.             printf("]\nPuncte pe linia orizontală y = %d: [%d", currentY, points[i].x);
  41.         } else {
  42.             printf(", %d", points[i].x);
  43.         }
  44.     }
  45.     printf("]\n");
  46.  
  47.     free(points);
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement