fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. typedef struct _Position {
  7. int x;
  8. int y;
  9. }Position;
  10.  
  11. bool cmp(const Position& p1, const Position& p2)
  12. {
  13. if (p1.y == p2.y) //좌표 y가 같다면
  14. return p1.x < p2.x; //좌표 x를 기준으로 올림차순
  15. else
  16. return p1.y < p2.y; //좌표 y를 기준으로 올림차순
  17. }
  18.  
  19. int main(void)
  20. {
  21. ios::sync_with_stdio(false);
  22. cin.tie(NULL);
  23.  
  24. int n;
  25. cin >> n;
  26. Position* arr = new Position[n];
  27.  
  28. for (int i = 0; i < n; i++)
  29. cin >> arr[i].x >> arr[i].y;
  30.  
  31. sort(arr, arr + n, cmp);
  32.  
  33. for (int i = 0; i < n; i++)
  34. cout << arr[i].x << " " << arr[i].y << "\n";
  35.  
  36. delete[] arr;
  37. return 0;
  38. }
Success #stdin #stdout 0s 5044KB
stdin
5
0 4
1 2
1 -1
2 2
3 3
stdout
1 -1
1 2
2 2
3 3
0 4