본문 바로가기

ALGORITHM/SWEA

[SWEA D1] 1945. 간단한 소인수분해(Python)

문제 링크

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

 

SW Expert Academy

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

swexpertacademy.com


제출 코드

- Pass

T = int(input())
# 여러개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
for test_case in range(1, T + 1):
    N = int(input())
    a = b = c = d = e = 0
    while N % 2 == 0:
        N = N / 2
        a = a + 1
    while N % 3 == 0:
        N = N / 3
        b = b + 1
    while N % 5 == 0:
        N = N / 5
        c = c + 1
    while N % 7 == 0:
        N = N / 7
        d = d + 1
    while N % 11 == 0:
        N = N / 11
        e = e + 1
    print(f"#{test_case} {a} {b} {c} {d} {e}")

 


풀이

  • 주어진 N이 2, 3, 5, 7, 11로 나누어떨어지는 한 계속 각 숫자로 나누고 나눈 횟수만큼 a~e의 값으로.

알아둘 것 & 생각해볼 것