작성일 :

문제 링크

5354번 - J박스

설명

입력으로 주어진 크기에 맞춰 특정한 문자 패턴의 박스를 출력하는 문제입니다.

  • 출력해야 하는 박스는 다음 규칙을 따릅니다:
    • 테두리는 모두 #으로 채웁니다.
    • 안쪽은 문자 J로 채웁니다.
    • 단, 크기가 1인 경우에는 #만 출력합니다.
  • 여러 개의 테스트케이스가 주어지며, 각 박스는 한 줄씩 빈 줄로 구분되어야 합니다.

접근법

  • 테스트케이스 수를 입력받고, 각 케이스에 대해 반복합니다.
  • 한 줄씩 출력하면서:
    • 맨 윗줄과 맨 아랫줄은 모두 #로 출력합니다.
    • 그 외 줄은 # + (J 반복) + #의 형식으로 출력합니다.
  • 각 테스트케이스 사이에는 빈 줄을 출력합니다 (단, 마지막은 제외).

Code

[ C# ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

class Program {
  static void Main() {
    int t = int.Parse(Console.ReadLine());
    while (t-- > 0) {
      int n = int.Parse(Console.ReadLine());
      for (int i = 0; i < n; i++) {
        if (i == 0 || i == n - 1)
          Console.WriteLine(new string('#', n));
        else
          Console.WriteLine("#" + new string('J', n - 2) + "#");
      }
      if (t > 0) Console.WriteLine();
    }
  }
}



[ C++ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t; cin >> t;
  while (t--) {
    int n; cin >> n;

    for (int i = 0; i < n; i++) {
      if (i == 0 || i == n - 1) cout << string(n, '#') << "\n";
      else cout << "#" << string(n - 2, 'J') << "#" << "\n";
    }

    if (t > 0) cout << "\n";
  }

  return 0;
}