개발
[99클럽] 알고리즘 TIL: 백준 10798번 세로읽기 - JavaScript
soyooooon
2025. 1. 18. 01:15
반응형
문제링크
https://www.acmicpc.net/problem/10798
풀이방법
- input으로 들어오는 string들의 길이가 제각각일 수 있기 때문에, 1차로 input의 길이 중 max 값을 구한다.
- 가장 긴 길이에 맞춰서 이중 for문을 돌려 앞에서부터 차례대로 세로 글자들이 쌓이게끔 result를 업데이트한다.
- 최종 업데이트 된 result를 출력한다.
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", (line) => {
input.push(line);
});
rl.on("close", () => {
const maxLength = Math.max(...input.map((str) => str.length));
let result = "";
for (let i = 0; i < maxLength; i++) {
for (let j = 0; j < input.length; j++) {
if (input[j][i] === undefined) continue;
result += input[j][i];
}
}
console.log(result);
});
반응형