ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준2577 배열- 숫자의 개수
    알고리즘/백준 문제 2019. 8. 19. 00:33

    문제를 보고 0~9를 배열에 넣어주고 곱해진 숫자의 각 자리수를 배열 인덱스 하나하나 비교한뒤  또 다른 배열을 하나  만들어서 각 수자의 개수를 저장하면 되겠다고 생각은 했다.

    고민한것은 어떻게 곱해진 수의 각 자리수를 분리할것인가 였는데 10으로 나누고 나머지를 이용하여 쉽게 해결했다. 

    나머지수를 비교한뒤, total 값을 10으로 나누고, 나누어진 값을 다시 %10 하여 비교해주면 된다.

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class Main {
     
        public static void main(String[] args) throws NumberFormatException, IOException {
            
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int a = Integer.parseInt(br.readLine());
        int b = Integer.parseInt(br.readLine());
        int c = Integer.parseInt(br.readLine());
        int total = a*b*c;
        
        int[] arr = new int[]{0,1,2,3,4,5,6,7,8,9} ;
        int[] answer = new int[] {0,0,0,0,0,0,0,0,0,0} ;
        
        int check_end;
        
        do {
            for(int i=0; i<arr.length; i++) {
                check_end=total%10;
                if(check_end == arr[i]) answer[check_end%10]++;
            }
            total/=10;
        }while(total != 0);
        
        for(int i=0; i<answer.length; i++System.out.println(answer[i]);
     
        }    
    }
     
    cs

    댓글

Designed by Tistory.