본문 바로가기

ALGORITHM/SWEA

[SWEA D1] 2029. 몫과 나머지 출력하기(Python)

문제 링크

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=1&contestProbId=AV5QGNvKAtEDFAUq&categoryId=AV5QGNvKAtEDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=1&pageSize=10&pageIndex=2 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com


제출 코드

- Pass

T = int(input())
# 여러개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
for test_case in range(1, T + 1):
    # ///////////////////////////////////////////////////////////////////////////////////
    a, b = map(int, input().split())
    x = a // b
    y = a % b
    print(f'#{test_case} {x} {y}')

풀이

  • 변수 a, b를 받아서 a를 b로 나눈 몫을 x, 나머지를 y로 받아 출력

알아둘 것

T = int(input())
# 여러개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
for test_case in range(1, T + 1):
    # ///////////////////////////////////////////////////////////////////////////////////
    a, b = map(int, input().split())
    # 이렇게도 가능하다.
    x, y = divmod(a, b)
    print(f'#{test_case} {x} {y}')
  • divmod(x, y) : x를 y로 나눈 몫과 나머지를 tuple로 반환하는 함수