[파이썬][백준 9713번] Sum of Odd Sequence
1. 문제Permalink
[Bronze III] Sum of Odd Sequence - 9713Permalink
성능 요약Permalink
메모리: 30840 KB, 시간: 72 ms
분류Permalink
사칙연산(arithmetic), 수학(math)
문제 설명Permalink
Given an odd integer N, calculate the sum of all the odd integers between 1 and N inclusive.
입력Permalink
First line of the input contains T, the number of test cases. Each test case contains a single integer N. N is between 1 and 100.
출력Permalink
For each test case output the value 1+3+….+N.
출처: 백준, https://https://www.acmicpc.net/
2. 해결방법 시간복잡도Permalink
- 단순 코딩 O(N^2)
3. 문제 해결 및 코드Permalink
N = int(input()) | |
for _ in range(N): | |
N = int(input()) | |
print(sum([i for i in range(1, N + 1) if i % 2])) |
-
주석을 참고하면서 이해를 돕습니다.Permalink
4. 알고리즘 및 해설Permalink
- 반복문을 통해 값을 입력받는다.
- 입력받은 값을 컴프리핸션을 통해 만약 해당 값까지 돌아가는 중 위치 값이 짝수일 경우 리스트에 넣어준다.
- 이후 최종 리스트의 합을 출력한다.