나머지가 1이 되는 수 찾기, IntSummaryStatistics

박경희·2023년 12월 24일

코딩테스트

목록 보기
5/69

Lv.1 - 나머지가 1이 되는 수 찾기

 public int solution(int n) {
         int answer = 0;
        int x = 1;
        if (3 <= n && n <= 1000000) {
            while (n % x != 1) {
                x++;
            } answer = x;
        }
        return answer;
    }

IntSummaryStatistics

IntSummaryStatistics 객체는 최소값, 최대값, 합계, 평균 등 여러 통계 정보를 제공한다.

import java.util.Arrays;
import java.util.IntSummaryStatistics;

public static String solution(String s) {
    IntSummaryStatistics stats = Arrays.stream(s.split(" "))
                                       .mapToInt(Integer::parseInt)
                                       .summaryStatistics();

    return stats.getMin() + " " + stats.getMax();
}

0개의 댓글