728x90
반응형
아래는 처음 풀이 코드이다. 정답이긴 하지만 코드가 쓸데없이 길어서 짜면서 답답함을 느꼈다.
def easy(n):
s = 0
num, cnt = 1, 1
while(1):
for _ in range(num):
if cnt == n+1:
break
s += num
cnt += 1
num += 1
if cnt == n+1:
break
return s
A, B = map(int, input().split())
print(easy(B) - easy(A-1))
아래는 최종 풀이 코드이다.
위의 과정을 리스트의 합을 사용해 간단하게 구현했다.
A, B = map(int, input().split())
li = []
for i in range(1, 46):
li += [i]*i
print(sum(li[A-1:B]))
728x90
반응형
'Agorithm > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 1453번 피시방 알바(python) (0) | 2020.02.04 |
---|---|
백준 알고리즘 1350번 진짜 공간(python) (0) | 2020.02.04 |
백준 알고리즘 1237번 정ㅋ벅ㅋ(python) (0) | 2020.02.03 |
백준 알고리즘 1026번 보물(python) (0) | 2020.02.03 |
백준 알고리즘 1929번 소수 구하기(python) (0) | 2020.01.29 |