Programming/Python & Data Structures
[Baekjoon/Python3] 4673번 셀프 넘버
HooNeee
2020. 12. 4. 17:38
[Baekjoon/Python3] 4673번 셀프 넘버
4673번: 셀프 넘버
셀프 넘버는 1949년 인도 수학자 D.R. Kaprekar가 이름 붙였다. 양의 정수 n에 대해서 d(n)을 n과 n의 각 자리수를 더하는 함수라고 정의하자. 예를 들어, d(75) = 75+7+5 = 87이다. 양의 정수 n이 주어졌을 때,
www.acmicpc.net
def d():
not_self = []
num = 0
for i in range(1, 10001): # 1 ~ 9999
if i < 10: # 한자리수
num = i + i
not_self.append(num)
elif i < 100: # 두자리수
num = i + (i // 10) + (i % 10)
not_self.append(num)
elif i < 1000: # 세자리수
num = i + (i // 100) + ((i % 100) // 10) + ((i % 100) % 10)
not_self.append(num)
elif i < 10000: # 네자리수
num = i + (i // 1000) + ((i % 1000) // 100) + (((i % 1000) % 100) // 10) + (((i % 1000) % 100) % 10)
if num <= 10000:
not_self.append(num) # 생성자 예외처리
not_self.sort() # 오름차순
self = [res for res in range(1, 10001) if res not in not_self] # if ~ not in ~
for self_num in self:
print(self_num)
d()