728x90

Code

😢 기본 문제
😊 JavaScript built-in method인 sort()를 사용해주면 된다.
문자를 정렬할 때는 sort()를 그냥 써줘도 되지만, 숫자를 정렬할 때는 sort((a, b) => a-b); 로 써줘야 한다.
난 for ... of를 사용하고 좋아하지만, 속도(?)와 참고하는 사람들 때문에 정석 버전으로 캡쳐했다.
Full Code (https://github.com/DasolPark/Dasol_JS_Algorithm/tree/master/Baekjoon)
| // Sort numbers(Ascending order) |
| // For submit |
| // const fs = require('fs'); |
| // const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n').map(num => parseInt(num)); |
| // For local test |
| const input = [5, 5, 2, 3, 4, 1]; |
| const N = input.shift(); |
| const sorted = input.sort((a, b) => a - b); |
| for (let i = 0; i < N; i++) { |
| console.log(sorted[i]); |
| } |
| // For submit |
| // const fs = require('fs'); |
| // const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n').map(num => parseInt(num)); |
| // For local test |
| // const input = [5, 5, 2, 3, 4, 1]; |
| // input.shift(); |
| // const sorted = input.sort((a, b) => a - b); |
| // for (let num of sorted) { |
| // console.log(num); |
| // } |
'Algorithm > JavaScript(Node.js)' 카테고리의 다른 글
| 백준 2231번: 분해합 Node.js(JavaScript) (0) | 2020.02.03 |
|---|---|
| 백준 2798번: 블랙잭 Node.js(JavaScript) (0) | 2020.02.01 |
| 백준 7568번: 덩치 Node.js(JavaScript) (0) | 2020.01.29 |
| 백준 1002번: 터렛 Node.js(JavaScript) (0) | 2020.01.28 |
| 백준 3053번: 택시 기하학(유클리드 기하학) Node.js(JavaScript) (0) | 2020.01.28 |