[백준 14539] Grid Pattern (C#, C++) - soo:bak
작성일 :
문제 링크
설명
행과 열의 개수, 그리고 각 칸의 너비와 높이가 주어질 때, 조건에 맞는 텍스트 격자를 출력하는 문제입니다.
접근법
격자는 두 종류의 줄만 반복해서 만들면 됩니다.
가로 경계선은 +--...-+ 형태이고, 칸 내부 줄은 |**...*| 형태입니다. 먼저 이 두 줄을 한 번 만든 뒤, 각 행마다 가로 경계선 한 줄과 내부 줄 h개를 출력하면 됩니다.
모든 행을 처리한 뒤 마지막 가로 경계선을 한 번 더 출력하면 격자가 완성됩니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Text;
class Program {
static void Main() {
int t = int.Parse(Console.ReadLine()!);
var sb = new StringBuilder();
for (int tc = 1; tc <= t; tc++) {
string[] input = Console.ReadLine()!.Split();
int row = int.Parse(input[0]);
int col = int.Parse(input[1]);
int w = int.Parse(input[2]);
int h = int.Parse(input[3]);
string horizontal = BuildHorizontal(col, w);
string inside = BuildInside(col, w);
sb.Append("Case #").Append(tc).AppendLine(":");
for (int r = 0; r < row; r++) {
sb.AppendLine(horizontal);
for (int k = 0; k < h; k++) {
sb.AppendLine(inside);
}
}
sb.AppendLine(horizontal);
}
Console.Write(sb.ToString());
}
static string BuildHorizontal(int col, int w) {
var line = new StringBuilder();
for (int c = 0; c < col; c++) {
line.Append('+');
for (int i = 0; i < w; i++) {
line.Append('-');
}
}
line.Append('+');
return line.ToString();
}
static string BuildInside(int col, int w) {
var line = new StringBuilder();
for (int c = 0; c < col; c++) {
line.Append('|');
for (int i = 0; i < w; i++) {
line.Append('*');
}
}
line.Append('|');
return line.ToString();
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
string buildHorizontal(int col, int w) {
string line;
for (int c = 0; c < col; c++) {
line += '+';
line += string(w, '-');
}
line += '+';
return line;
}
string buildInside(int col, int w) {
string line;
for (int c = 0; c < col; c++) {
line += '|';
line += string(w, '*');
}
line += '|';
return line;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
for (int tc = 1; tc <= t; tc++) {
int row, col, w, h;
cin >> row >> col >> w >> h;
string horizontal = buildHorizontal(col, w);
string inside = buildInside(col, w);
cout << "Case #" << tc << ":\n";
for (int r = 0; r < row; r++) {
cout << horizontal << "\n";
for (int k = 0; k < h; k++) {
cout << inside << "\n";
}
}
cout << horizontal << "\n";
}
return 0;
}