์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step11] 2231 : ๋ถ„ํ•ดํ•ฉ (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 18. 17:52

www.acmicpc.net/problem/2231

 

2231๋ฒˆ: ๋ถ„ํ•ดํ•ฉ

์–ด๋–ค ์ž์—ฐ์ˆ˜ N์ด ์žˆ์„ ๋•Œ, ๊ทธ ์ž์—ฐ์ˆ˜ N์˜ ๋ถ„ํ•ดํ•ฉ์€ N๊ณผ N์„ ์ด๋ฃจ๋Š” ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค. ์–ด๋–ค ์ž์—ฐ์ˆ˜ M์˜ ๋ถ„ํ•ดํ•ฉ์ด N์ธ ๊ฒฝ์šฐ, M์„ N์˜ ์ƒ์„ฑ์ž๋ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 245์˜ ๋ถ„ํ•ดํ•ฉ์€ 256(=245+2+4+5)์ด

www.acmicpc.net


์ˆซ์ž x๊ฐ€ ์ „๋‹ฌ๋˜๋ฉด x์˜ ๋ถ„ํ•ดํ•ฉ์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜ add๋ฅผ ์ž‘์„ฑ

1. 1๋ถ€ํ„ฐ n๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ๋ถ„ํ•ดํ•ฉ์„ ๊ตฌํ•ด ๋ถ„ํ•ดํ•ฉ์ด n๊ณผ ๊ฐ™์œผ๋ฉด t = i๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋ฃจํ”„ ํƒˆ์ถœ

   ( n์˜ ๊ฐ€์žฅ ์ž‘์€ ์ƒ์„ฑ์ž๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 1๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰ )

2. t๋ฅผ ์ถœ๋ ฅ

   ( ์ƒ์„ฑ์ž๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด t๋Š” ๊ทธ๋Œ€๋กœ 0์ด๊ธฐ ๋•Œ๋ฌธ์— 0์ด ์ถœ๋ ฅ๋จ )

 

# 2231.py

def add(x):
    a = list(map(int, str(x)))
    return x + sum(a)


n = int(input())
t = 0

for i in range(1, n+1):
    if n == add(i):
        t = i
        break

print(t)