[알고리즘] 백준 1789 : 수들의 합 - S5

eternal moment·2023년 5월 25일
0

2023.05.25 풀이

import sys
input=sys.stdin.readline

s=int(input())
sum=0
cnt=0

for i in range(0, s):
    sum+=i
    if sum>s:
        cnt=i-1
        break
    elif sum==s:
        cnt=i
        break
if s==1 or s==2:
    print(1)
else:
    print(cnt)

2023.11.10 풀이

import sys
input=sys.stdin.readline

s=int(input())

i=1
while True:
    s-=i
    if s==0:
        print(i)
        break
    elif s<0:
        print(i-1)
        break
    i+=1


다른 풀이

1

s = int(input())
k = 0
j = 0
while(s>k):
    j += 1
    k += j
if (k>s):
    j -= 1
print(j)

2

a = int(input())
i = 0
while a>=0:
    i += 1
    a -= i
print(i-1)

3 풀이참고

n = int(input())
i = 0
cnt = 0
while True:
    if n > i: # n이 i보다 크면 n에 i를 차감
        i += 1
        n = n-i
        cnt += 1
    else:
        print(cnt)
        break


check point

  • 1, 2 일때 10보다 작을 때 등 예외조건 생각하면서 풀기

0개의 댓글