본문 바로가기

Programming/algorithm

백준 2577 java

풀이과정

입력 받은 수, 결과 값의 자리 수 만큼 배열 생성.

substring 이용. 한자리씩 잘라 넣음. (지금 생각해보니 %10 하면 substring 안써도 될듯...)

for문 과 if 문 활용해서 j와 값이 같아질 때마다 countarr[해당숫자]에 +1씩 더 함.

import java.io.*;

public class t2577 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int a = Integer.parseInt(br.readLine());
        int b = Integer.parseInt(br.readLine());
        int c = Integer.parseInt(br.readLine());
        int result = 0;
            result = a*b*c;

        int idx = String.valueOf(result).length();
        String arr[] = new String[idx];
        int count = 0;
        int countarr[] = new int[10];
            for(int i = 0 ; i < idx ; i++) {
                if (0 < i || i < idx) {
                    arr[i] = String.valueOf(result).substring(i, i + 1);
                }
                for (int j = 0; j < 10; j++) {
                    if (j == Integer.parseInt(arr[i])) {
                        countarr[j] += 1;
                    }
                }
            }
            for(int k = 0; k < 10 ; k++){
                bw.write(countarr[k] + "\n");
            }
            br.close();
            bw.flush();
            bw.close();
    }

 

 

'Programming > algorithm' 카테고리의 다른 글

백준 1932 자바 DP 풀이  (0) 2020.07.15
백준 2579 자바 풀이  (0) 2020.07.15
백준 1904 자바 DP 풀이  (0) 2020.07.14
[APIO 2010] 특공대 (Commando) 풀이  (0) 2020.06.23
2017 kakao blind test question_1  (0) 2020.05.04