(BOJ10798 세로읽기

Link copied to clipboard

문제 설명

Link copied to clipboard
  • 2중 for문으로 탐색하며, 각 줄의 길이가 달라 조건문이 추가되는 문제
  • 출력은 한줄로 이어서 하면 되므로 크게 신경쓸 부분은 없다

문제 풀이

Link copied to clipboard

Python

Link copied to clipboard
seq = [input().strip() for _ in range(5)]
ans = ""
for j in range(15):
    for i in range(5):
        if len(seq[i]) < j + 1:
            continue
        ans += seq[i][j]
print(ans)
  • 다른 언어 풀이도 유사하게 풀었다.
  • 5줄을 먼저 입력받고, 출력 문자열을 선언한 다음
    • 왼쪽부터 세로로 순회하며 문자열 길이를 초과하지 않을 때 출력 문자열에 추가한다.

Go

Link copied to clipboard
var seq []string
for i := 0; i < 5; i++ {
    sc.Scan()
    tmp := sc.Text()
    seq = append(seq, tmp)
}
for j := 0; j < 15; j++ {
    for i := 0; i < 5; i++ {
        if j < len(seq[i]) {
            wr.WriteByte(seq[i][j])
        }
    }
}
  • Go 문자열은 immutable이므로 바로바로 출력하는 방식 사용

Node.js

Link copied to clipboard
let seq = []
for (let i = 0; i < 5; i++) {
  seq.push(input[i])
}
let ans = ""
for (let j = 0; j < 15; j++) {
  for (let i = 0; i < 5; i++) {
    if (seq[i].length > j) {
      ans += seq[i][j]
    }
  }
}
  • Python과 유사하게 풀이