IT/Programming / / 2022. 7. 22. 13:52

[백준, Python] 1934번 최소공배수

반응형
 

최소공배수 성공

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

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB 44306 25150 21454 58.223%

문제

두 자연수 A와 B에 대해서, A의 배수이면서 B의 배수인 자연수를 A와 B의 공배수라고 한다. 이런 공배수 중에서 가장 작은 수를 최소공배수라고 한다. 예를 들어, 6과 15의 공배수는 30, 60, 90등이 있으며, 최소 공배수는 30이다.

두 자연수 A와 B가 주어졌을 때, A와 B의 최소공배수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 테스트 케이스의 개수 T(1 ≤ T ≤ 1,000)가 주어진다. 둘째 줄부터 T개의 줄에 걸쳐서 A와 B가 주어진다. (1 ≤ A, B ≤ 45,000)

출력

첫째 줄부터 T개의 줄에 A와 B의 최소공배수를 입력받은 순서대로 한 줄에 하나씩 출력한다.

예제 입력 1 복사

3
1 45000
6 10
13 17

예제 출력 1 복사

45000
30
221

 


 

더보기
# 1차 입력값 처리
def chkInput(caseCntInput, testCaseList=None):
    # 케이스 개수 체크 (1 ~ 1000)
    if not (1 <= caseCntInput <= 1000):
        raise Exception('케이스 갯수가 잘못됐습니다.')

    # 케이스 반복 횟수 체크
    for testCase in testCaseList:
        caseLength = len(testCase)

        # 입력값이 두개가 아닐 때
        if caseLength != 2:
            raise Exception('입력값이 2개가 아닙니다.')

        if not (1 <= testCase[0] <= 45000) or not (1 <= testCase[1] <= 45000):
            raise Exception('입력값의 범위가 잘못되었습니다.')


# 결과값 계산
def calOutput(testCaseList=None):
    funcResultList = []

    for testCase in testCaseList:
        numberA = int(testCase[0]) # 1번째 수
        numberB = int(testCase[1]) # 2번째 수

        # 2부터 순차적으로 곱해 서로 나눠지는 최소 값을 구함
        for mulIndex in range(1, (numberA * numberB)+1):
            if (numberA * mulIndex) % numberB == 0:
                funcResultList.append(numberA * mulIndex)
                break

    return funcResultList


try:
    caseCntInput = int(input())
    caseList = []
    for index in range(0, caseCntInput):
        caseList.append(list(map(int, input().split())))

    chkInput(caseCntInput, caseList)

    resultList = calOutput(caseList)
    for index in resultList:
        print(index)

except Exception as e:
    print(e)
반응형

'IT > Programming' 카테고리의 다른 글

[백준, Python] 4101번 크냐?  (0) 2022.07.22
[백준, Python] 10156번 과자  (0) 2022.07.22
[백준, Python] 2480번 주사위 세개  (0) 2022.07.22
[백준, Python] 10039번 평균 점수  (0) 2022.07.11
[백준, Python] 2753번 윤년  (0) 2022.07.11
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유