본문 바로가기
ALGORITHM/SW Expert Academy

[SWEA/Python] 1945. 간단한 소인수분해

by 안녕나는현서 2021. 8. 9.
728x90

📌 문제

https://swexpertacademy.com/main/main.do

 

SW Expert Academy

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

swexpertacademy.com

 

📌 코드

T = int(input())

for t in range(1, T+1) :
    N = int(input())

    result = []

    while N != 1 :
        if not N%2 :
            result.append(2)
            N /= 2
        elif not N%3 :
            result.append(3)
            N /= 3
        elif not N%5 :
            result.append(5)
            N /= 5
        elif not N%7 :
            result.append(7)
            N /= 7
        elif not N%11 :
            result.append(11)
            N /= 11

    print(f'#{t}', end = ' ')

    for a in [2, 3, 5, 7, 11] :
        if a == 11 :
            print(f'{result.count(a)}')
        else :
            print(f'{result.count(a)}', end = ' ')

 

+) 0810 추가

import sys

sys.stdin = open('input.txt')

T = int(input())

for t in range(1, T+1) :
    N = int(input())

    fact = [2, 3, 5, 7, 11]
    result = [0]*5

    while N > 1 :
        for i in range(len(fact)) :
            if not N%fact[i] :
                result[i] += 1
                N /= fact[i]

    print(f'#{t}', end = ' ')

    for j in range(len(fact)) :
        if fact[j] == 11 :
            print(f'{result[j]}')
        else :
            print(f'{result[j]}', end = ' ')

반복되는 부분들을 반복문으로 변경해보았다!

코드가 더 깔끔하긴 한데 메모리나 시간은 거의 차이가 없었다.

728x90

'ALGORITHM > SW Expert Academy' 카테고리의 다른 글

[SWEA/Python] 1206. View  (0) 2021.08.13
[SWEA/Python] 1954. 달팽이 숫자  (0) 2021.08.09
[SWEA/Python] 6217. 객체지향 3  (0) 2021.07.30
[SWEA/Python] 6311. 내장함수 4  (0) 2021.07.24
[SWEA/Python] 6326. 함수의 기초 7  (0) 2021.07.22

댓글