본문 바로가기
2022 하반기/Python

백준 2231 python

by concho 2023. 1. 29.

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

 

2231번: 분해합

어떤 자연수 N이 있을 때, 그 자연수 N의 분해합은 N과 N을 이루는 각 자리수의 합을 의미한다. 어떤 자연수 M의 분해합이 N인 경우, M을 N의 생성자라 한다. 예를 들어, 245의 분해합은 256(=245+2+4+5)이

www.acmicpc.net

def generator(n):
    strN = str(n)
    for i in range(0,len(strN)):
        n += int(strN[i])
    return n

N = input()
iN = int(N)
if iN <= 10:
    if iN % 2 == 0:
        print(int(iN/2))
    else:
        print(0)
else :
    minSol = iN - 9 * (len(N)-1) - int(N[0])

    for i in range(minSol, iN) :
        if generator(i) == iN :
            print(i)
            break
        elif i == iN-1:
            print(0)

ex)

N = 216일 경우

216 - 2(첫째 자릿수) -  2(자릿수-1) * 9 =>  196

minSol = 196부터 탐색 시작

*/

예외처리 => 10 이하의 경우 2로 안나눠지면 생성자x

탐색결과 생성자가 없는경우도 생성자x

*/

'2022 하반기 > Python' 카테고리의 다른 글

백준 1436 Python  (0) 2023.02.07
백준 1018 python  (0) 2023.02.02
백준 7568 python  (2) 2023.02.02
백준 2798 python  (0) 2023.01.26
백준 python  (0) 2023.01.26

댓글