[Softeer] 7628번 연탄의 크기 - Java

yseo14·2025년 1월 23일

코딩테스트 대비

목록 보기
46/88


문제링크

풀이

n이 최대 100이기 때문에 완전탐색으로 풀이해도 시간이 충분하다.
주어진 집들의 난로 반지름을 배열에 저장 후, 2부터 100까지 반복문을 돌면서 난로 반지름과 모듈러 연산을 하여 0인 경우에는 count를 증가시킨다. 연탄의 반지름에 대하여 모든 난로 반지름과 연산을 한 후에 count와 max를 비교하며 max값을 업데이트한다.

코드

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

public class Main {
    static int n; 
    static int max = 0;
    static int count;
    static int[] arr;
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        arr = new int[n];
        StringTokenizer st = new StringTokenizer(br.readLine());
        for(int i = 0;i < n; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        for(int i = 2; i <= 100; i++){
            count = 0;
            int curr = i;
            for(int j = 0; j < n; j++){
                if(arr[j] % curr == 0){
                   count++;
                }
            }
            max = Math.max(max, count);
        }

        System.out.println(max);
    }
}
profile
like the water flowing

0개의 댓글