작성일 :

문제 링크

10990번 - 별 찍기 - 15

설명

각 줄마다 공백의 개수와 별의 위치 규칙을 파악하여 특정 패턴으로 별을 출력하는 문제입니다.


접근법

  • 먼저 정수 N을 입력받습니다.
  • 0번째 줄부터 N - 1번째 줄까지 반복합니다.
    • 각 줄의 앞쪽에는 n - i - 1개의 공백이 들어갑니다.
    • 첫 번째 줄은 별 하나만 출력합니다.
    • 두 번째 줄부터는 양 끝에 별을 두고, 그 사이에 2 * i - 1개의 공백을 출력합니다.



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 n = int.Parse(Console.ReadLine());

    for (int i = 0; i < n; i++) {
      Console.Write(new string(' ', n - i - 1));
      Console.Write("*");
      if (i > 0) {
        Console.Write(new string(' ', 2 * i - 1));
        Console.Write("*");
      }
      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
23
#include <bits/stdc++.h>

using namespace std;

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

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

  return 0;
}