SWEA 1945 간단한 소인수분해

1 개요[ | ]

SWEA 1945 간단한 소인수분해

2 C++[ | ]

#include <iostream>
using namespace std;
int main() {
    int T;
    scanf("%d", &T);
    int N;
    for(int tc=1; tc<=T; tc++) {
        scanf("%d", &N);
        int a=0, b=0, c=0, d=0, e=0;
        while( N%2 == 0 ) { N/=2; a++; }
        while( N%3 == 0 ) { N/=3; b++; }
        while( N%5 == 0 ) { N/=5; c++; }
        while( N%7 == 0 ) { N/=7; d++; }
        while( N%11 == 0 ) { N/=11; e++; }
        printf("#%d %d %d %d %d %d\n",tc,a,b,c,d,e);
    }
}

3 Java[ | ]

import java.util.Scanner;
public class Solution {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int tc=1; tc<=T; tc++) {
            int n = sc.nextInt();
            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;
            int e = 0;
            while( n%2 == 0 ) { n /= 2; a++; }
            while( n%3 == 0 ) { n /= 3; b++; }
            while( n%5 == 0 ) { n /= 5; c++; }
            while( n%7 == 0 ) { n /= 7; d++; }
            while( n%11 == 0 ) { n /= 11; e++; }
            System.out.format("#%d %d %d %d %d %d\n", tc, a, b, c, d, e);
        }
    }
}

4 Python[ | ]

T = int(input())

for t in range(1, T+1):
    a, b, c, d, e = 0, 0, 0, 0, 0
    num = int(input())
    
    while num % 2 == 0:
        a += 1
        num //= 2
    while num % 3 == 0:
        b += 1
        num //= 3
    while num % 5 == 0:
        c += 1
        num //= 5
    while num % 7 == 0:
        d += 1
        num //= 7
    while num % 11 == 0:
        e += 1
        num //= 11
        
    print('#{} {} {} {} {} {}'.format(t, a, b, c, d, e))

# github.com/wansang93/Algorithm
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}