๋ฐฑ์ค€ 2447 in python

ํ™์œค๊ธฐยท2022๋…„ 9์›” 24์ผ
0

์žฌ๊ท€์ ์ธ ํŒจํ„ด์œผ๋กœ ๋ณ„์„ ์ฐ์–ด ๋ณด์ž.
N์ด 3์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ(3, 9, 27, ...)์ด๋ผ๊ณ  ํ•  ๋•Œ,
ํฌ๊ธฐ N์˜ ํŒจํ„ด์€ Nร—N ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋‹ค.

ํฌ๊ธฐ 3์˜ ํŒจํ„ด์€ ๊ฐ€์šด๋ฐ์— ๊ณต๋ฐฑ์ด ์žˆ๊ณ ,
๊ฐ€์šด๋ฐ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์นธ์— ๋ณ„์ด ํ•˜๋‚˜์”ฉ ์žˆ๋Š” ํŒจํ„ด์ด๋‹ค.

N์ด 3๋ณด๋‹ค ํด ๊ฒฝ์šฐ,
ํฌ๊ธฐ N์˜ ํŒจํ„ด์€ ๊ณต๋ฐฑ์œผ๋กœ ์ฑ„์›Œ์ง„ ๊ฐ€์šด๋ฐ์˜
(N/3)ร—(N/3) ์ •์‚ฌ๊ฐํ˜•์„ํฌ๊ธฐ N/3์˜ ํŒจํ„ด์œผ๋กœ ๋‘˜๋Ÿฌ์‹ผ ํ˜•ํƒœ์ด๋‹ค.

๐Ÿ”Ž ๋ฌธ์ œ ํŒŒ์•…


์ดํ•ด๋ฅผ ๋•๊ธฐ์œ„ํ•œ ์˜ˆ์‹œ๋ฅผ ๋œฏ์–ด ๋ณด์ž.
1. ์žฌ๊ท€์  ํŒจํ„ด์œผ๋กœ ๋ณ„์ด ์ฐํ˜€์žˆ๋‹ค.
2. ํฌ๊ธฐ๊ฐ€ 3์ธ ํŒจํ„ด์„ ๊ธฐ๋ณธ์œผ๋กœ
3. ์žฌ๊ท€๋ฅผ ๊ฑฐ๋“ญํ•  ์ˆ˜๋ก ํŒจํ„ด์ด ์ปค์ง„๋‹ค. (๋นจ๊ฐ„์ƒ‰ -> ์ดˆ๋ก์ƒ‰ -> ํŒŒ๋ž€์ƒ‰ ๋ฐ•์Šค)

ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ ํŒจํ„ด์„ ์žก๊ณ  ์žฌ๊ท€ ํ˜ธ์ถœ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒจํ„ด์„ ํ‚ค์›Œ๋‚˜๊ฐˆ ๊ฒƒ์ด๋‹ค.

๐Ÿ”‘recursive call

๊ธฐ๋ณธ์ ์ธ ํ‹€์„ ๋จผ์ € ์žก์•„๋ณด๊ฒ ๋‹ค.

# ํฌ๊ธฐ ์ž…๋ ฅ ๋ฐ›๊ธฐ
N = int(input())

#์žฌ๊ท€ ํ•จ์ˆ˜ ๊ฒฐ๊ณผ๋ฌผ ์ถœ๋ ฅ
for i in starprinting(N):
		print(i)

# pattern ๋ฐ˜๋ณต
def starprinting(N: int):
	# ๊ธฐ๋ณธ ํŒจํ„ด ์ •์˜
    if N == 3:
    	pattern = ///
    
    # ํฌ๊ธฐ๊ฐ€ N > 3 ์ผ ๋•Œ, ํŒจํ„ด ์ •์˜ (์žฌ๊ท€ ํ•จ์ˆ˜ ์‚ฌ์šฉ)
	new_star = ///
    
    # ๋ฐ˜ํ™˜
    return new_star

๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ํ˜•ํƒœ์ธ ํฌ๊ธฐ๊ฐ€ 3์ธ ํŒจํ„ด์„ list๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.
๋‹จ, pattern์— ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ํฌํ•จํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค. ๊ฐœํ–‰๋ฌธ์ž ํฌํ•จ์‹œ ์ดํ›„์— ์—ฐ๊ฒฐ๋  ์ฝ”๋“œ๊ฐ€ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.

pattern = ["***", "* *", "***"]

N์ด 3์ธ ๊ธฐ๋ณธ ํŒจํ„ด์— ๋‹ค๋ฅธ ํŒจํ„ด์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

new_star = 	[i * 3 for i in starprinting(N // 3)] + \
			[i + ' ' * (N // 3) + i for i in starprinting(N // 3)] + \
			[i * 3 for i in starprinting(N // 3)]

๊ฐ™์€ ํŒจํ„ด์„ ์ƒ๋‹จ์— 3๋ฒˆ, ์ค‘๋‹จ์— ๊ณต๋ณต์„ ํฌํ•จํ•˜์—ฌ 2๋ฒˆ, ํ•˜๋‹จ์— 3๋ฒˆ ์ž…๋ ฅํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ตฌ์กฐ๋กœ ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ํ˜•ํƒœ์ด๋‹ค.

๐Ÿš€ ์ตœ์ข… ์ฝ”๋“œ

์ตœ์ข… ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

N = int(input())

for i in starprinting(N):
		print(i)

# pattern ๋ฐ˜๋ณต
def starprinting(N: int):
	if N == 3:
		# pattern์— ๊ฐœํ–‰ ๋ฌธ์ž ๋„ฃ์œผ๋ฉด ์•ˆ๋จ.
		pattern = ["***", "* *", "***"]
		return pattern
	new_star = [i * 3 for i in starprinting(N // 3)] + \
			   [i + ' ' * (N // 3) + i for i in starprinting(N // 3)] + \
			   [i * 3 for i in starprinting(N // 3)]
	return new_star

0๊ฐœ์˜ ๋Œ“๊ธ€