Post

[BOJ] 2292. ๋ฒŒ์ง‘(python)

๐Ÿ“Œ๋ฌธ์ œ

Alt text

๐Ÿ’ช์•„์ด๋””์–ด

  1. ์ง‘ํ•ฉ ๋ฌถ๊ธฐ
    1์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ์ž…๋ ฅ๊ฐ’์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฉ๋ฌธํ•˜๋Š” ์ตœ์†Œ์˜ ๋ฐฉ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž…๋ ฅ๊ฐ’์ด ๋ช‡ ๋ฒˆ์งธ ๊ทธ๋ฃน์— ์†ํ•˜๋Š”์ง€ ์•Œ์•„์•ผํ•œ๋‹ค.
    [1], [2,3,4,5,6,7],[8,9,10,..,19],[20,...,37],...
    ์œผ๋กœ ์ง‘ํ•ฉ์ด ๋ฌถ์ธ๋‹ค. ๊ฐ ์ง‘ํ•ฉ์˜ ์ฒซ ๋ฒˆ์งธ ์ˆ˜๋Š” 1,2,8,20,...์œผ๋กœ 1,6,12,18,...์”ฉ ๋Š˜์–ด๋‚œ๋‹ค. ์ „ ์ฒซ๋ฒˆ์งธ ์ˆซ์ž+6(n-2) ์˜ ๊ทœ์น™์„ฑ์ด ์žˆ๋‹ค.

  2. ์ž…๋ ฅ๊ฐ’์ด ์†ํ•˜๋Š” ์ง‘ํ•ฉ์˜ ์ธ๋ฑ์Šค ์ฐพ๊ธฐ
    ์ž…๋ ฅ๊ฐ’์ด ๋‹ค์Œ ์ง‘ํ•ฉ์˜ ์ฒซ ๋ฒˆ์งธ๋ณด๋‹ค ์ž‘์„ ๋•Œ ์ง‘ํ•ฉ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์˜ ์ ํ™”์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž…๋ ฅ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’์„ ์ฐพ์•„ ์ง‘ํ•ฉ์˜ ์ธ๋ฑ์Šค -1ํ•œ๋‹ค.

๐Ÿฅ‚์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
n=int(input())
arr=[0,1,2]
if n==1: print(1)
else:
    answer=3
    while arr[-1]<=n:
        arr.append(arr[-1] + 6*(answer-2))
        answer+=1
    print(answer-2)
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.