![전체 글](https://t1.daumcdn.net/tistory_admin/static/manage/images/r3/default_L.png)
전체 글
[백준 1021] 회전하는 큐 with Node.js
문제 링크 www.acmicpc.net/problem/1021 1021번: 회전하는 큐 첫째 줄에 큐의 크기 N과 뽑아내려고 하는 수의 개수 M이 주어진다. N은 50보다 작거나 같은 자연수이고, M은 N보다 작거나 같은 자연수이다. 둘째 줄에는 지민이가 뽑아내려고 하는 수의 위치가 www.acmicpc.net 풀이 const [n, m, ...arr] = require("fs").readFileSync("/dev/stdin").toString().trim().split(/\s/).map(v => +v); let count = 0; function Node(value) { this.value = value; this.prevNode = null; this.nextNode = null; } function..
[백준 10816] 숫자 카드 2 with Node.js
문제 링크 www.acmicpc.net/problem/10816 10816번: 숫자 카드 2 첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10, www.acmicpc.net 풀이 const input = require("fs").readFileSync("/dev/stdin").toString().trim().split(/\s/); const n = parseInt(input[0]); const n_arr = input.slice(1, n+1); const [m, ...m_arr] = input.slice(n+1); const myMap ..
[백준 2217] 로프 with Node.js
문제 링크 www.acmicpc.net/problem/2217 2217번: 로프 N(1 ≤ N ≤ 100,000)개의 로프가 있다. 이 로프를 이용하여 이런 저런 물체를 들어올릴 수 있다. 각각의 로프는 그 굵기나 길이가 다르기 때문에 들 수 있는 물체의 중량이 서로 다를 수도 있다. 하 www.acmicpc.net 풀이 let [n, ...ropes] = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n"); ropes = ropes.map(i => +i); ropes.sort((a, b) => a-b); let min = ropes[0]*n; for (let i=1; i
[백준 1436] 영화감독 숌 with Node.js
문제 링크 www.acmicpc.net/problem/1436 1436번: 영화감독 숌 666은 종말을 나타내는 숫자라고 한다. 따라서, 많은 블록버스터 영화에서는 666이 들어간 제목을 많이 사용한다. 영화감독 숌은 세상의 종말 이라는 시리즈 영화의 감독이다. 조지 루카스는 스타 www.acmicpc.net 풀이 const n = +require("fs").readFileSync("/dev/stdin").toString() let num = 666; let count = 1; while (count !== n) { num++; if (String(num).includes("666")) count++; } console.log(num); num을 1씩 늘려가면서 num내에 연속된 666이 존재할 때마다 ..
[백준 1018] 체스판 다시 칠하기 with Node.js
문제 링크 www.acmicpc.net/problem/1018 1018번: 체스판 다시 칠하기 첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다. www.acmicpc.net 풀이 const [n, m, ...board] = require("fs").readFileSync("/dev/stdin").toString().trim().split(/\s/); const lines = ["WBWBWBWB", "BWBWBWBW"] let min = 64; for (let i=0; i
[백준 2108] 통계학 with Python
문제 링크 www.acmicpc.net/problem/2108 2108번: 통계학 첫째 줄에 수의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 그 다음 N개의 줄에는 정수들이 주어진다. 입력되는 정수의 절댓값은 4,000을 넘지 않는다. www.acmicpc.net 풀이 import sys n = int(sys.stdin.readline()) lst = [0 for x in range(8001)] for i in range(n): lst[int(sys.stdin.readline())+4000] += 1 # 산술평균 print(round(sum([(x-4000)*lst[x] for x in range(8001)])/n)) # 중앙값 a, b, mid = 0, 0, n//2+1 if lst[0] >=..
Node.js로 백준(BOJ) 문제 풀 때 유의할 점들
백준에서 Node.js로 입력을 받는 방법은 크게 두 가지가 있다. 첫 번째는 readline 모듈을 사용하는 것이고, 두 번째는 fs 모듈을 사용하는 것이다. (이 글에서는 fs 모듈에 대해서만 다루겠다.) Python으로 백준 문제를 풀 때 시간 초과가 나는 것을 막기 위해 input() 대신 sys.stdin.readline()을 쓰는 것처럼, Node.js도 readline보다 fs모듈을 사용하는 것이 더 빠르다. var fs = require('fs'); var input = fs.readFileSync('/dev/stdin').toString().split(' '); var a = parseInt(input[0]); var b = parseInt(input[1]); console.log(a+b..
[백준 10814] 나이순 정렬 with Node.js
문제 링크 www.acmicpc.net/problem/10814 10814번: 나이순 정렬 온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을 www.acmicpc.net 풀이 const [n, ...members] = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n"); console.log( members .map(v => ({age: parseInt(v.split(" ")[0]), name: v.split(" ")[1]})) .sort((a, b) => a.age - b.age) .map(v =..
[백준 11650] 좌표 정렬하기 with Node.js
문제 링크 www.acmicpc.net/problem/11650 11650번: 좌표 정렬하기 첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다. www.acmicpc.net 풀이 const [n, ...coords] = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n"); console.log( coords .map(v => ({x: parseInt(v.split(" ")[0]), y: parseInt(v.split(" ")[1])})) .so..
[백준 10815] 숫자 카드 with Python
문제 링크 www.acmicpc.net/problem/10815 10815번: 숫자 카드 첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10, www.acmicpc.net 풀이 import sys n = int(sys.stdin.readline()) n_lst = list(map(int, sys.stdin.readline().split())) m = int(sys.stdin.readline()) m_lst = list(map(int, sys.stdin.readline().split())) def binary_search(lst, n): l..