![[백준 / BOJ] 11650번 좌표 정렬하기 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FxrYa5%2FbtrJn6KqAlX%2FzfknnVLgZddksAQUXxSA60%2Fimg.png)
[백준 / BOJ] 11650번 좌표 정렬하기 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 5. 31. 18:51
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/11650
11650번: 좌표 정렬하기
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
www.acmicpc.net
문제

문제 풀이
C++ 상세 풀이
C++ 풀이 펼쳐보기
pair 클래스를 선언해서 해결했다. 보통 C++로 오면 vector에 pair을 활용할 때가 많은 듯 하다.
class pair {
public:
int x;
int y;
};
bool cmp(pair a, pair b) {
if (a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
Python 상세 풀이
Python 풀이 펼쳐보기
2차원 리스트를 활용했다.
for i in range(N):
x, y = map(int, input().split())
arr.append([x, y])
arr.sort()
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
class pair { | |
public: | |
int x; | |
int y; | |
}; | |
bool cmp(pair a, pair b) { | |
if (a.x == b.x) | |
return a.y < b.y; | |
else | |
return a.x < b.x; | |
} | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
int N; | |
std::cin >> N; | |
std::vector<pair> v(N); | |
for (int i = 0; i < N; ++i) | |
std::cin >> v[i].x >> v[i].y; | |
std::sort(v.begin(), v.end(), cmp); | |
for (int i = 0; i < N; ++i) | |
std::cout << v[i].x << " " << v[i].y << "\n"; | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
input = sys.stdin.readline | |
N = int(input()) | |
arr = [] | |
for i in range(N): | |
x, y = map(int, input().split()) | |
arr.append([x, y]) | |
arr.sort() | |
for x, y in arr: | |
print(x, y) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 1181번 단어 정렬(C++, Python) (0) | 2022.06.01 |
---|---|
[백준 / BOJ] 11651번 좌표 정렬하기 2(C++, Python) (0) | 2022.06.01 |
[백준 / BOJ] 1427번 소트인사이드 (C++, Python) (0) | 2022.05.31 |
[백준 / BOJ] 2108번 통계학 (C++, Python) (0) | 2022.05.31 |
[백준 / BOJ] 10989번 수 정렬하기 3 (C++, Python) (0) | 2022.04.14 |
@Reo :: 코드 아카이브
자기계발 블로그