반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
05-19 00:03
관리 메뉴

ImJay

[파이썬/Python] 백준 26545번 Mathematics 본문

Solved.ac - Python/Bronze V

[파이썬/Python] 백준 26545번 Mathematics

ImJay 2023. 6. 5. 04:14
반응형

[파이썬/Python] 백준 26545번 Mathematics

 

26545번: Mathematics

A mathematician has stolen your calculator! Luckily, you know how to code and can write a program that adds together numbers. Write a program that adds together a list of integers.

www.acmicpc.net


문제

A mathematician has stolen your calculator! Luckily, you know how to code and can write a program that adds together numbers. Write a program that adds together a list of integers.

한 수학자가 당신의 계산기를 훔쳤습니다! 다행히도, 코드를 작성하고 숫자들을 더할 수 있는 프로그램을 알고 있습니다. 정수들의 리스트를 더하는 프로그램을 작성하세요.

입력

The first line will contain a single integer n that indicates the number of integers to add together. The next n lines will each contain one integer. Your task is to write a program that adds all of the integers together.

첫 번째 줄에는 정수 n이 주어지며, 더할 정수의 개수를 나타냅니다. 다음 n개의 줄에는 각각 하나의 정수가 포함됩니다. 모든 정수를 더하는 프로그램을 작성해야 합니다.

출력

Output the resulting integer. The output should be one line containing one integer value.

결과 정수를 출력하세요. 출력은 한 줄에 하나의 정수 값이 있어야 합니다

예제 입력

3
1
2
3

예제 출력

6

풀이

# 더할 정수의 개수를 입력
n = int(input())

# 합을 저장할 변수를 초기화
sum = 0

# n번 반복하여 정수를 입력받고 합에 더함
for _ in range(n):
    # 정수를 입력받습니다.
    num = int(input())
    # 합에 입력받은 정수를 더함
    sum += num
    
# 최종 합을 출력
print(sum)

이 프로그램의 시간 복잡도는 O(n)이다. n개의 정수를 입력받고, 각 정수를 더하기 때문에 입력 크기에 비례하여 실행 시간이 증가한다. 따라서 입력 크기에 따라 선형적으로 실행 시간이 증가한다.

반응형
Comments