백준/브론즈3

[Python] 백준 3507번 - Automated Telephone Exchange

두부마라탕 2025. 4. 15. 16:45

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


문제

In St Petersburg phone numbers are formatted as “XXX–XX–XX”, where the first three digits represent index of the Automated Telephone Exchange (ATE). Each ATE has exactly 10 000 unique phone numbers.

Peter has just bought a new flat and now he wants to install a telephone line. He thinks that a phone number is lucky if the arithmetic expression represented by that phone number is equal to zero. For example, the phone number 102–40–62 is lucky (102 − 40 − 62 = 0), and the number 157–10–47 is not lucky (157 − 10 − 47 = 100 ≠ 0).

Peter knows the index of the ATE that serves his house. To get an idea of his chances to get a lucky number he wants to know how many lucky numbers his ATE has.


간단히 말하자면 전화번호 xxx-xx-xx가 있는데 xxx - xx - xx = 0이 나오면 lucky 한 거고 0이 되지 않으면 unlucky 한 번호다. 앞에 세 자릿수를 입력받았을 때 럭키할 수 있는 경우는 몇 가지가 있는지 구하는 문제이다.

 

 

코드

num = int(input())

lucky = 0
if num < 199:
    for i in range(100):
        for j in range(100):
            if i+j == num:
                lucky += 1
    print(lucky)
else:
    print(0)

 

풀이

1. 세 자릿수를 받아준다.
2. 두 자릿수끼리만 더해서 나올 수 있는 최대 수는 198(99+99)이다. 그 때문에 198을 기준으로 if와 else를 나누어주었다.
3. 이제 이중 반복문을 사용하여 i+j가 num일 때 경우의 수를 1 더하도록 했다.
4. 반복문이 끝난 후 가능한 경우의 수를 출력하도록 했다.