BOJ

#2577(java)

Sloth Coder 2022. 7. 2. 17:02

Solution1)

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {

        int A = 0, B = 0, C = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        A = Integer.parseInt(br.readLine());
        B = Integer.parseInt(br.readLine());
        C = Integer.parseInt(br.readLine());

        int result = A * B * C;
        ArrayList<Integer> nums = new ArrayList<>(); //result가 몇자리 수일지 알 수 없으므로 크기가 유동적인 리스트 사용.

        while(result > 0){
            nums.add(result % 10);
            result /= 10;
        }

        int [] numsArr = nums.stream().mapToInt(Integer::intValue).toArray();
        //mapToInt(class::method) -> class에 있는 method를 이용해 값들을 하나씩 변환
        //mapToInt(i -> function) -> 값들을 i에 받아와서 function으로 변환. ex) function = i+1

        int [] check = new int[10]; //초기화 없이 선언시 배열 값 0으로 초기화

        for(int i : numsArr)
            check[i]++;

        for(int k : check)
            System.out.println(k);
    }
}

 

Solution2)

import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception {

        int A = 0, B = 0, C = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        A = Integer.parseInt(br.readLine());
        B = Integer.parseInt(br.readLine());
        C = Integer.parseInt(br.readLine());

        int result = A * B * C;
        int [] check = new int[10]; //초기화 없이 선언시 배열 값 0으로 초기화

        while(result > 0){
            check[result % 10]++;
            result /= 10;
        }

        for(int k : check)
            System.out.println(k);
    }
}

 

메모리

: Sol1 > Sol2

 

시간

: Sol2(124ms) = Sol1(124ms)