작성일 :

문제 링크

2445번 - 별 찍기 - 8

설명

입력으로 주어지는 N개의 줄에 대해, 좌우 대칭 구조의 별 패턴을 출력하는 문제입니다.

출력은 총 2 * N - 1줄로 구성되며, 위아래 대칭 구조로 이루어져 있습니다.

  • 위쪽은 점차 별이 증가하고 공백이 감소합니다.
  • 중간 줄은 별이 2 * N개 출력됩니다.
  • 아래쪽은 위쪽과 반대로 별이 감소하고 공백이 증가합니다.

접근법

  • 상단 부분:
    • N - 1줄 반복
    • 왼쪽 별: i가 증가함에 따라 *의 개수는 i
    • 가운데 공백: 2 * (N - i)
    • 오른쪽 별: i
  • 중간 줄:
    • 2 * N개의 별을 출력
  • 하단 부분:
    • 상단과 대칭
    • 왼쪽 별: N - i
    • 가운데 공백: 2 * i
    • 오른쪽 별: N - i

Code

[ C# ]

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

namespace Solution {
  class Program {
    static void Main(string[] args) {
      int n = int.Parse(Console.ReadLine()!);
      for (int i = 1; i < n; i++) {
        Console.Write(new string('*', i));
        Console.Write(new string(' ', 2 * (n - i)));
        Console.WriteLine(new string('*', i));
      }
      Console.WriteLine(new string('*', 2 * n));
      for (int i = 1; i < n; i++) {
        Console.Write(new string('*', n - i));
        Console.Write(new string(' ', 2 * i));
        Console.WriteLine(new string('*', n - i));
      }
    }
  }
}



[ 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
#include <bits/stdc++.h>

using namespace std;

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

  int n; cin >> n;
  for (int i = 1; i < n; i++) {
    for (int j = n - i; j < n; j++) cout << "*";
    for (int j = i; j < n; j++) cout << " ";
    for (int j = i; j < n; j++) cout << " ";
    for (int j = n - i; j < n; j++) cout << "*";
    cout << "\n";
  }

  for (int i = 0; i < 2 * n; i++) cout << "*";

  cout << "\n";
  for (int i = 1; i < n; i++) {
    for (int j = i; j < n; j++) cout << "*";
    for (int j = n - i; j < n; j++) cout << " ";
    for (int j = n - i; j < n; j++) cout << " ";
    for (int j = i; j < n; j++) cout << "*";
    cout << "\n";
  }

  return 0;
}