728x90
6975번: Deficient, Perfect, and Abundant
Write a program that repeatedly reads a positive integer, determines if the integer is deficient, perfect, or abundant, and outputs the number along with its classification. A positive integer, n, is said to be perfect if the sum of its proper diviso
www.acmicpc.net
문제 풀이
주어진 입력값을 조건에 따라 출력해준다. 먼저, 입력값으로 자기 자신을 제외한 약수의 리스트를 구해준다.
- 약수 리스트의 합이 입력값보다 작으면 "n is a deficient number." 출력
- 약수 리스트의 합이 입력값과 같으면 "n is a perfect number." 출력
- 약수 리스트의 합이 입력값보다 크면 "n is an abundant number." 출력
테스트케이스마다 개행 한줄을 출력해준다.
정답 코드
import sys
input = sys.stdin.readline
for _ in range(int(input())) :
n = int(input())
lst = [i for i in range(1, n) if n % i == 0]
if sum(lst) < n : print(n, "is a deficient number.")
elif sum(lst) == n : print(n, "is a perfect number.")
else : print(n, "is an abundant number.")
print()
728x90
'Algorithm > PS' 카테고리의 다른 글
[BOJ/백준] 7523 - Gauß [python] (0) | 2023.03.20 |
---|---|
[BOJ/백준] 7510 - 고급 수학 [python] (1) | 2023.03.20 |
[BOJ/백준] 6974 - Long Division [python] (0) | 2023.03.20 |
[BOJ/백준] 6903 - Trident [python] (0) | 2023.03.19 |
[BOJ/백준] 6889 - Smile with Similes [python] (1) | 2023.03.19 |