inblog logo
|
LHS's Study Space
    C#알고리즘문제풀기

    [알고리즘 문제 풀기] 별 찍기 - 12(2522)

    C#
    lhs's avatar
    lhs
    Apr 06, 2025
    [알고리즘 문제 풀기] 별 찍기 - 12(2522)
    Contents
    1. 문제 풀이 아이디어2. 나의 정답 코드3. 정리
    www.acmicpc.net
    https://www.acmicpc.net/problem/2522
    notion image

    1. 문제 풀이 아이디어

    • 위쪽 삼각형과 아래쪽 삼각형을 각각 반복문으로 만들어 공백과 별의 개수를 조절하여 다이아몬드 형태로 출력해 문제를 해결할 수 있다.

    2. 나의 정답 코드

    using System.Text; StringBuilder sb = new StringBuilder(""); using (StreamReader sr = new StreamReader(Console.OpenStandardInput())) using (StreamWriter sw = new StreamWriter(Console.OpenStandardOutput())) { int n = int.Parse(sr.ReadLine()); for (int i = 1; i <= n; i++) { sb.Append(' ', n - i); sb.Append('*', i); sb.AppendLine(); } for (int i = n - 1; i >= 1; i--) { sb.Append(' ', n - i); sb.Append('*', i); sb.AppendLine(); } sw.Write(sb); }

    3. 정리

    • 첫 번째 for문에서 위쪽 삼각형을 만들어 공백은 n - i개, 별은 i개 출력한다.
    • 두 번째 for문에서 아래쪽 삼각형을 만들어 공백은 n - i개, 별은 i개 출력한다.
    Share article
    Contents
    1. 문제 풀이 아이디어2. 나의 정답 코드3. 정리

    LHS's Study Space

    RSS·Powered by Inblog