Algorithm/Baekjoon

셀프 넘버(4673번 문제) Python3 답안

Bonita SY 2020. 2. 9. 21:19
728x90
반응형

문제)

답안)

self_number_list = [i for i in range(1, 10001)]

def find_self_number(n):
  sum_val = n

  for str_n in str(n):
    sum_val += int(str_n)

  if (sum_val > 10000):
    return

  if (sum_val in self_number_list):
    self_number_list.remove(sum_val)

def main():
  for i in range(1, 10000):
    find_self_number(i)

  for snl in self_number_list:
    print(snl)

if __name__=="__main__":
  main()

시간초과 난 답)

ㅋㅋ 지나친 걱정이 문제

self_number_list = [i for i in range(1, 10001)]

def find_self_number(n):
  if (n > 10000):
    return

  sum_val, one, ten, hun, tho = 0, 0, 0, 0, 0

  if (n < 10):
    one = n
  elif (n < 100):
    one = int(n%10)
    ten = int(n/10)
  elif (n < 1000):
    one = int(n%10)
    ten = int((n%100)/10)
    hun = int(n/100)
  else:
    one = int(n%10)
    ten = int((n%100)/10)
    hun = int((n%1000)/100)
    tho = int(n/1000)

  sum_val = n + one + ten + hun + tho

  if (sum_val > 10000):
    return

  if (sum_val in self_number_list):
    self_number_list.remove(sum_val)

  find_self_number(sum_val)

def main():
  for i in range(1, 10000):
    find_self_number(i)

  for snl in self_number_list:
    print(snl)

if __name__=="__main__":
  main()
728x90
반응형