본문 바로가기
Study/[Algorithm]

[백준] 2577번 JAVA - 숫자의 개수

by Nameless 2021. 5. 21.

 

 

 

 


 

1. Scanner 사용

 - number.charAt() - '0' : char 에서 int 변환한다.(가장 간단한 방법)

 

 

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int value=(sc.nextInt() * sc.nextInt() * sc.nextInt());
        String number = Integer.toString(value);
        
        for(int i=0; i<10; i++) {
            int count=0;
            for(int j=0; j<number.length(); j++) {
                if((number.charAt(j) - '0')==i) {
                    count ++;
                    }
            }
            System.out.println(count);
        }
        
    }
    
}

 

 

2. BufferedReader 사용

 - str.charAt() - '0'   or   str.charAt() - 48 으로 정수 변환

 

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int[] num = new int[10];
        
        int value = Integer.parseInt(br.readLine()) * Integer.parseInt(br.readLine()) * Integer.parseInt(br.readLine());
        String str = String.valueOf(value);
        
        for(int i=0; i<str.length(); i++) {
            num[(str.charAt(i) - '0')]++;
        }
        
        for(int v : num) {
            System.out.println(v);
        }
    }
    
}
 

댓글