[백준 4673 파이썬] 셀프 넘버

일단 해볼게·2022년 8월 29일
0

백준

목록 보기
19/132

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

# 셀프 넘버

def d(n): # 생성자를 이용해 d(n) 구하기
    result = n
    for i in str(n):
        result += int(i)
    return result

non_self_num = set() # 셀프넘버가 아닌 수

for i in range(1,10001):
    non_self_num.add(d(i)) # d(n)을 이용해 셀프넘버가 아닌 수를 구한다.

for i in range(1, 10001):
    if i not in non_self_num: # 셀프넘버 출력
        print(i)

다른 사람 d(n) 구하는 방법
def d(n):
    # 생성자 n을 이용해 d(n)을 만드는 수식
    n = n + sum( map(int, str(n)) )
    return n

int로 들어온 n을 str으로 형변환을 시켜주면 문자열 즉, iterable이 되고 그걸 map 함수를 이용해 다시 int로 바꾸면 sum()함수를 이용해 각 자리 수의 합을 구할 수 있다.


셀프 넘버가 아닌 수를 저장해 셀프 넘버를 구한다.
set()은 중복된 값을 제거한다.

참고
https://kbwplace.tistory.com/69

profile
시도하고 More Do하는 백엔드 개발자입니다.

0개의 댓글