쉽고 깔끔하게
[백준 알고리즘/python3] 8393번 합 문제풀이 본문
728x90
반응형

문제
n이 주어졌을 때, 1부터 n까지 합을 구하는 프로그램을 작성하시오.
입력
첫째 줄에 n (1 ≤ n ≤10,000)이 주어진다.
출력
1부터 n까지 합을 출력한다.
예제 입력 | 예제 출력 |
3 | 6 |
<정답>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = int(input()) | |
sum = 0 | |
for i in range(n+1): | |
sum += i | |
print(sum) |
※ for문 전에 sum을 0으로 초기화해주는 것이 핵심이다.
728x90
반응형
'Algorithm > BAEKJOON' 카테고리의 다른 글
[백준 알고리즘/python3] 2741번 N 찍기 문제풀이 (0) | 2021.01.03 |
---|---|
[백준 알고리즘/python3] 15552번 빠른 A+B 문제풀이 (0) | 2021.01.03 |
[백준 알고리즘/python3] 10950번 A+B -3 문제풀이 (0) | 2020.12.31 |
[백준 알고리즘/python3] 2739번 구구단 문제풀이 (0) | 2020.12.31 |
[백준 알고리즘/python3] 2884번 알람 시계 문제풀이 (0) | 2020.12.29 |