1 ๋ถ„ ์†Œ์š”

๋ฐฑ์ค€ ์‚ฌ์ดํŠธ ๋งํฌ

๋ฌธ์ œ

์„œ๋กœ ๋‹ค๋ฅธ N๊ฐœ์˜ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ์ด S๋ผ๊ณ  ํ•œ๋‹ค. S๋ฅผ ์•Œ ๋•Œ, ์ž์—ฐ์ˆ˜ N์˜ ์ตœ๋Œ“๊ฐ’์€ ์–ผ๋งˆ์ผ๊นŒ?

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ S(1 โ‰ค S โ‰ค 4,294,967,295)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

200

์˜ˆ์ œ ์ถœ๋ ฅ 1

19

๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋ง‰ํ˜”๋˜ ๋ถ€๋ถ„

๋ฌธ์ œ์—์„œ๋Š” S = 1๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋”ํ•ด๊ฐˆ ๋•Œ, ๊ทธ ํ•ฉ์ด N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์•„์ง€๋ฉด์„œ ๊ฐ€์žฅ ํฐ S๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋กœ ์ด๋ฅผ ์ˆ˜์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด 1๋ถ€ํ„ฐ S๊นŒ์ง€์˜ ํ•ฉ์ด N ์ดํ•˜์ธ ๊ฐ€์žฅ ํฐ S๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” S๋ฅผ 1๋ถ€ํ„ฐ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ, 1๋ถ€ํ„ฐ S๊นŒ์ง€์˜ ํ•ฉ์„ ๊ตฌํ•˜๊ณ  ์ด๊ฒƒ์ด N ์ดํ•˜์ธ์ง€ ๊ฒ€์‚ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 1๋ถ€ํ„ฐ S๊นŒ์ง€์˜ ํ•ฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณต์‹์œผ๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1 + 2 + โ€ฆ + S = S * (S+1) / 2

์ฝ”๋“œ

ver(1) - math ์‚ฌ์šฉ

import math

n = int(input())
s = math.floor((-1 + math.sqrt(1 + 8*n)) / 2)
print(s)

ver(2)

N = int(input())     # N์„ ์ž…๋ ฅ๋ฐ›์Œ

temp = 1             # ๋ณ€์ˆ˜ temp์— 1์„ ํ• ๋‹น
answer = 0           # ๋ณ€์ˆ˜ answer์— 0์„ ํ• ๋‹น

while True:          # ๋ฌดํ•œ๋ฃจํ”„
    N -= temp        # N์—์„œ temp๋ฅผ ๋บŒ

    if N >= 0:       # ๋งŒ์•ฝ N์ด 0 ์ด์ƒ์ด๋ฉด
        answer += 1  # answer์— 1์„ ๋”ํ•˜๊ณ 
        temp += 1    # temp์— 1์„ ๋”ํ•จ
    else:            # N์ด 0๋ณด๋‹ค ์ž‘์•„์ง€๋ฉด
        print(answer) # ํ˜„์žฌ๊นŒ์ง€์˜ answer๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋ฃจํ”„๋ฅผ ๋น ์ ธ๋‚˜๊ฐ
        break

ver(3)

# sys ๋ชจ๋“ˆ์„ importํ•˜์—ฌ stdin์„ ํ†ตํ•ด ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.
import sys

# ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›์Šต๋‹ˆ๋‹ค.
n = int(sys.stdin.readline())

# ํ•ฉ๊ณ„์™€ ๊ฒฐ๊ณผ๊ฐ’์„ ์ €์žฅํ•  ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
current_sum = 0
count = 0  # ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•  ๋ณ€์ˆ˜๋ช…์„ 'count'๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

# 1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ์ˆ˜๋ฅผ ๋”ํ•˜๋ฉด์„œ ์กฐ๊ฑด์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
for num in range(1, n + 1):
    current_sum += num  # ํ˜„์žฌ๊นŒ์ง€์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
    count += 1  # ์ˆ˜ํ–‰๋œ ํšŸ์ˆ˜๋ฅผ ์„ธ๊ธฐ ์œ„ํ•ด count ๋ณ€์ˆ˜๋ฅผ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.

    # ํ˜„์žฌ๊นŒ์ง€์˜ ํ•ฉ์ด ์ฃผ์–ด์ง„ ์ˆ˜ n์„ ์ดˆ๊ณผํ•˜๋ฉด ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฏ€๋กœ count ๊ฐ’์„ ์กฐ์ •ํ•˜๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
    if current_sum > n:
        count -= 1  # ์ดˆ๊ณผํ•œ ๊ฒฝ์šฐ ํ•œ ๋ฒˆ ๋” ๋”ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ count์—์„œ 1์„ ๋นผ์ค๋‹ˆ๋‹ค.
        break

# ์ตœ์ข… ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
print(count)

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ