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

업데이트:



문제 정보


풀이

문제

The impossible has happened. Bear G. has fallen into his own trap. Lured by a delicious box of Domaćica, without even thinking, he rushed and fell into his trap. In order to get out of the trap, he must solve the following task with your help. You are given three integers L, D i X.

  • determine the minimal integer N such that L ≤ N ≤ D and the sum of its digits is X

  • determine the maximal integer M such that L ≤ M ≤ D and the sum of its digits is X

Bear will be able to escape from the trap if he correctly determines numbers N and M. …

입력 요약
The first line of input contains the integer L (1 ≤ L ≤ 10 000), the number from the task.

The second line of input contains the integer D (1 ≤ D ≤ 10 000, L ≤ D), the number from the task.

The third line of input contains the integer X (1 ≤ X ≤ 36), the number from the task.

출력 요약
The first line of output must contain the integer N from the task.

The second line of output must contain the integer M from the task

코드

l,d,x=(int(input())for _ in range(3))
n=m=0
for i in range(d-l+1):
    if n==0 and sum(map(int,str(l+i)))==x:n=l+i
    if m==0 and sum(map(int,str(d-i)))==x:m=d-i
    if n!=0!=m:break
print(n,m,sep='\n')

설명

핵심은 구현 관점에서 The impossible has happened. Bear G. has fallen into his own trap. Lured by a delicious box of Domaćica, without even thinking, he rushed and fell int …를 만족하도록 로직을 구성하는 것입니다.

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

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



다음 읽을거리

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

댓글남기기