๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 2292 - ๋ฒŒ์ง‘

by YWTechIT 2021. 6. 18.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 2292 - ๋ฒŒ์ง‘

๋ฐฑ์ค€ 2292 - ๋ฒŒ์ง‘


โšก๏ธ ๋‚˜์˜ ํ’€์ด

๊ณ„์ฐจ์ˆ˜์—ด์„ ์ˆ˜ํ•™์ ์œผ๋กœ ๊ตฌํ˜„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ฌป๋Š” ๋ฌธ์ œ์ด๋‹ค. ๊ทœ์น™์„ ์ž์„ธํžˆ๋ณด๋ฉด 1, 7, 19, 37, 81...์ธ๋ฐ ์—ฌ๊ธฐ์„œ ๊ณ„์ฐจ๋Š” 6์˜ ๋ฐฐ์ˆ˜๋งŒํผ ์ฆ๊ฐ€ํ•œ๋‹ค.


์ฆ‰, equation = equation + (6 * i)์—์„œ ๊ฒฐ๊ณผ๊ฐ’์— +1์”ฉ ๋”ํ•œ ๊ฐ’์ด ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— +1์„ ๋„ฃ์œผ๋ฉด +1์ด ๋“ค์–ด๊ฐ„์ฑ„๋กœ ๋ˆ„์ ๋˜๊ธฐ๋•Œ๋ฌธ์— ๊ฐ’์ด ๋‹ฌ๋ผ์ง„๋‹ค. ๋”ฐ๋ผ์„œ ์กฐ๊ฑด์ ˆ์—์„œ +1์„ ๋„ฃ๊ณ  n๊ณผ ๋น„๊ตํ–ˆ๋‹ค.

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ดค๋Š”๋ฐ while๋ฌธ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€์—ˆ๋‹ค. ๋†€๋ž๋‹ค. cnt๋ฅผ ๋”ฐ๋กœ ์“ธ ์ƒ๊ฐ์„ ํ•˜์ง€ ์•Š๊ณ  i๊ฐ’์œผ๋กœ ์ถœ๋ ฅ ํ• ์ˆ˜๋„ ์žˆ๊ตฌ๋‚˜๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์™œ 1์ผ๋•Œ์˜ ์กฐ๊ฑด์€ ๋”ฐ๋กœ ์•ˆ๋„ฃ์–ด์คฌ๋Š”์ง€ ์˜๋ฌธ์ด ์ƒ๊ฒผ๋Š”๋ฐ, 1์ผ๋•Œ๋Š” while๋ฌธ์— ๊ฑธ๋ฆฌ์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— ์ƒ๋žตํ–ˆ๋‹ค. ์™œ ๊ทธ ์ƒ๊ฐ์„ ํ•˜์ง€ ๋ชปํ–ˆ์„ ๊นŒ? (์ž…ํ‹€๋ง‰ ๐Ÿคญ ๐Ÿคญ)

 

# ๋‚˜์˜ ํ’€์ด
n = int(input())
equation = 0
cnt = 1

if n == 1:
    print(1)
else:
    for i in range(1, 100000):
        equation = equation + (6 * i)
        cnt += 1
        if n <= equation + 1:
            print(cnt)
            break

 

728x90
# ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด
n = int(input())
room = 1
cnt = 1

while n > room:
    room = room + (6 * cnt)
    cnt += 1
print(cnt)
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€