https://www.acmicpc.net/problem/11650
문제
2차원 평면 위의 점 N개가 주어진다. 좌표를 x좌표가 증가하는 순으로, x좌표가 같으면 y좌표가 증가하는 순서로 정렬한 다음 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.
출력
첫째 줄부터 N개의 줄에 점을 정렬한 결과를 출력한다.
조건정리
1. x좌표를 기준으로 오름차순 정렬
2. x좌표가 같다면 y좌표 기준으로 오름차순 정렬
코드
const fs = require("fs");
const file_path =
process.platform === "linux" ? "dev/stdin" : `${__dirname}/input.txt`;
const input = fs.readFileSync(file_path).toString().trim();
/**
* @param {string} input
* @returns
*/
const solution = (input) => {
const [_, ...coordi] = input.replace(/\r/g, "").split("\n");
const arr = coordi.map((el) => el.split(" ").map(Number));
const answer = arr
.sort((a, b) => {
if (!(a[0] - b[0])) {
return a[1] - b[1];
}
return a[0] - b[0];
})
.map(([x, y]) => `${x.toString()} ${y.toString()}`)
.join("\n");
return answer;
};
console.log(solution(input));
'코딩테스트 > 백준' 카테고리의 다른 글
17219. 맞힌 사람 - Node.js (0) | 2024.10.09 |
---|---|
11047. 동전 0 - Node.js (1) | 2024.10.07 |
2232. 분해합 - Node.js (4) | 2024.09.23 |
30802. 웰컴 키트 - Node.js (0) | 2024.09.20 |
1978. 소수 찾기 - Node.js (0) | 2024.09.20 |