[백준 알고리즘] 2231번 문제 분해합 구하기 Python

2021. 2. 2. 21:49

백준 알고리즘 2231번 문제 분해합구하기

2231번 문제 분해합 구하기이다.

 

구하려고 하는 수가 M이다.

어떤 자연수 N이 있을 때, 그 자연수의 분해합은 N은 M + M의 각 자리수별 숫자 를 더한 값이다.

 

1 <= N <= 1,000,000 이 주어지고 그에 맞는 M을 구하는 문제이다.

import sys

def getChar(M):
    sub_str_M_list = []
    while M:
        sub_str_M_list.append(int(M % 10)) 
        M = int(M / 10)
    return sum(sub_str_M_list)
        

def processing(N, M):
    result_M = 0
    for sub_M in range(M,N):
        if sub_M + getChar(sub_M) == N : result_M = sub_M
        else : continue
        break
    
    print(result_M)

def main(): 
    N = sys.stdin.readline().strip()
    int_N = int(N)
    if int_N > 10 :
        start_M = int_N - (len(N) * 9)
    else :
        start_M = 1
    processing(int_N, start_M)

if __name__ == '__main__':
    main()

 

나는 in으로 들어온 숫자에 대해 M이 될수 있는 값의 최소를 지정하고 그 최소부터 계산하여 가장 먼저 N과 계산값이

같아지는 수를 출력했다.

 

다른 분들의 코드를 보니 2줄이면 끝나던데.. 대단하시다들

 

BELATED ARTICLES

more