[JAVA] 백준 : 숫자의 개수

조예빈·2024년 8월 3일
0

Coding Test

목록 보기
91/138

https://www.acmicpc.net/problem/2577

숫자를 입력받으면서 나누고, 나눈 나머지를 배열에 저장해 주면 된다. 나는 단순히 42의 나머지의 개수라서 배열의 크기도 42로 설정해 주었다(나머지가 42를 넘어가는 경우는 없기 때문)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] result = new int[42];

        for (int i = 0; i < 10; i++) {
            int input = sc.nextInt();
            int idx = input % 42;
            result[idx]++;
        }
        int sum = 0;
        for (int i = 0; i < 42; i++) {
            if (result[i] != 0) {
                sum++;
            }
        }
        System.out.println(sum);
        sc.close();
    }
}

profile
컴퓨터가 이해하는 코드는 바보도 작성할 수 있다. 사람이 이해하도록 작성하는 프로그래머가 진정한 실력자다. -마틴 파울러

0개의 댓글