본문 바로가기
Baekjoon Algorithm/python

[Python]BAEKJOON 6491번 Perfection

by SeolLab. 2023. 3. 2.
728x90

https://www.acmicpc.net/problem/6491 

 

6491번: Perfection

From the article Number Theory in the 1994 Microsoft Encarta: "If a, b, c are integers such that a = bc, a is called a multiple of b or of c, and b or c is called a divisor or factor of a. If c is not 1, b is called a proper divisor of a. Even integers, wh

www.acmicpc.net

 

진짜 킹받게 하는 문제다.. 

 

'틀렸습니다'와 '맞았습니다' 사이 1시간동안 정말 많은 시행착오를 했다. 결론은 문제 입력값 조건 결여... 
입력이 한줄로 들어온다는 보장이 없다. 이게 사람을 환장하게 한다.

처음에 저 조건 모르고 아래 처럼 풀었다가 장렬히 전사했다. 

 

 

실패 Try1  '틀렸습니다'

N = list(map(int,input().split()))
for i in N[0:-1]:
    lst = []
    for j in range(1,i):
        if i%j == 0:lst.append(j)
    if sum(lst) == i: print(i, "PERFECT")
    elif sum(lst) > i: print(i, "ABUNDANT")
    elif sum(lst) < i: print(i, "DEFICIENT")

 

입력값이 한 줄로 안들어면 어떻게 들어오느냐. 

  15 
  28  232  
  6  12
  56 
  60000 
  22 496 0

이딴식으로 들어오는 게 가능하다. 

그래서 답이 아래와 같다. 

15 DEFICIENT
28 PERFECT
232 DEFICIENT
6 PERFECT
12 ABUNDANT
56 ABUNDANT
60000 ABUNDANT
22 DEFICIENT
496 PERFECT

 

덕분에 알게 된 조건과, extend()함수. 

평소에 리스트 내장 append()함수만 즐겨 사용했는데, 이 기회에 extend()함수를 쓸 수 있게 됐다. 

N =[]
while True:
    N.extend(list(map(int,input().split())))
    if 0 in N:
        N.remove(0)
        break

위 코드에서 extend()대신 append()를 쓰면 while루프를 절대 탈출하지 못한다. if 0 in N조건을 만족하는 경우가 절대 존재하지 않기 때문이다. 가령 입력값이 1 2 3 4라면, N은 [[1,2,3,4]]와 같은 식으로 저장된다. 이는 말 그대로 리스트를 기존 N리스트에 append시키기 때문이며, 이를 해결하기 위해 리스트를 extend(확장)시켜야 한다. extend를 쓰면 동일 입력에 대해 [1,2,3,4]로 저장된다. 

Sol

N =[]
while True:
    N.extend(list(map(int,input().split())))
    if 0 in N: 
        N.remove(0)
        break
for i in N: 
    lst = []
    for j in range(1,i):
        if i%j == 0:lst.append(j)
    if sum(lst) == i: print(i, "PERFECT")
    elif sum(lst) > i: print(i, "ABUNDANT")
    elif sum(lst) < i: print(i, "DEFICIENT")

 

댓글