본문 바로가기

알고리즘/문제 풀이

SW Expert Academy D2 1945. 간단한 소인수분해

SW Expert Academy

 

SW Expert Academy

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

swexpertacademy.com

import java.util.*;


class Solution
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();

        for (int test_case = 1; test_case <= T; test_case++) {
            int N = sc.nextInt();
            // list는 소인수 분해시 각 소수의 지수 값들을 저장할 것 이다.
            ArrayList<Integer> list = new ArrayList<>();

            // 작업
            primeFactor(list, N, 0);

            // 작업된 list 출력
            System.out.printf("#%d ", test_case);
            list.forEach(x-> System.out.printf("%d ", x));
            System.out.println();
        }
    }

    // prime 하나씩 N을 나눈다. 더 이상 깨끗하게 나눠 떨어지지 않을 때 까지 나누고,
    // 깨끗하게 나누어 떨어진 횟수를 list에 저장한다.
    static int primeFactor (ArrayList<Integer> list, int N, int i){
        if(i >4) {return 0;}
        int[] prime = new int[]{2,3,5,7,11};
        int count = 0;
        while(N%prime[i] == 0){
            if(N%prime[i] == 0) {
                count++;
                N /=prime[i];
            }
        }
        list.add(count);
        return primeFactor(list, N, ++i );


    }
}