728x90

소인수 분해를 이용하는 방식이다!

나는 while문을 사용해서 나머지가 0이 될때까지 나눈다는 것을 이용해서 문제를 풀었다.

 

풀이)

import java.util.Scanner;
import java.io.FileInputStream;
 

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
 
 
 
        for(int i=1; i<=N; i++){
            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;
            int e = 0;
            int s = sc.nextInt();
            while(s%2==0){
                s=s/2;
                a++;
            }
            while(s%3==0) {
                s=s/3;
                b++;
            }
            while(s%5==0){
                s=s/5;
                c++;
            }
            while(s%7==0) {
                s=s/7;
                d++;
            }
            while(s%11==0) {
                s=s/11;
                e++;
            }
 
            System.out.println("#"+i+" "+a+" "+b+" "+c+" "+d+" "+e);
 
        }
    }
}

for문을 한번 돌때마다 a, b, c, d, e의 값이 초기화 되도록 설정하였다.

그래서 2, 3, 5, 7, 11로 소인수 분해 할때의 주어진 값을 구할수 있도록 설정하였다.

728x90