[백준/파이썬] 16861번 풀이

업데이트:



문제 정보


풀이

문제

We’re all familiar with harshad numbers. For this problem, you will … what’s that? You aren’t familiar with harshad numbers? They’re also known as Niven numbers – does that ring a bell?? Anything???

Well, it’s a simple enough concept. A harshad number is a number which is evenly divisible by the sum of its digits. For example, 24 is a harshad number: the sum of its digits is 2 + 4 = 6 and 24 is divisible by 6. 156 is also a harshad number, since 1 + 5 + 6 = 12 and 156 = (12)(13). …

입력 요약
Input consists of a single line containing a positive integer n ≤ 1 000 000 000.

출력 요약
Display the smallest harshad number greater than or equal to n.

코드

import sys;read=sys.stdin.readline
n=int(read())
while True:
    if n%sum(map(int,str(n)))==0:break
    n+=1
print(n)

설명

핵심은 구현 관점에서 We’re all familiar with harshad numbers. For this problem, you will … …를 만족하도록 로직을 구성하는 것입니다.

코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.

경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.



다음 읽을거리

관련 허브 페이지에서 같은 주제의 글을 이어서 확인할 수 있습니다.

댓글남기기