본문 바로가기

카테고리 없음

[BOJ] 백준 2447 파이썬 - 별 찍기

728x90

<코드 1>

def rect_star(x, y, n, ls):
    if n == 3:
        ls[x + 1][y + 1] = " "
    else:
        temp = n // 3
        for i in range(x + temp, x + 2 * temp):
            for j in range(y + temp, y + 2 * temp):
                ls[i][j] = " "
        for i in range(0, n, temp):
            for j in range(0, n, temp):
                rect_star(x + i, y + j, temp, ls)


n = int(input())
ls = [["*" for i in range(n)] for i in range(n)]

rect_star(0, 0, n, ls)
for i in range(0, n):
    for j in range(0, n):
        print(ls[i][j], end="")
    print()

 

<코드 2>

def stars(len):
    if len == 1:
        return ['*']
    star = stars(len//3)
    l = []
    for s in star:
        l.append(s*3)
    for s in star:
        l.append(s + " "*(len//3) + s)
    for s in star:
        l.append(s*3)
    return l


n = int(input())
print('\n'.join(stars(n)))

 

728x90