본문 바로가기

코딩테스트/백준

[백준-자바] 5800번 성적 통계 / 2022.04.28

 

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

 

5800번: 성적 통계

첫째 줄에 중덕 고등학교에 있는 반의 수 K (1 ≤ K ≤ 100)가 주어진다. 다음 K개 줄에는 각 반의 학생수 N (2 ≤ N ≤ 50)과 각 학생의 수학 성적이 주어진다. 시험 성적은 0보다 크거나 같고, 100보다

www.acmicpc.net

 

 

import java.util.Scanner;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt(); // 반의 수


        int ban = 1; // 몇 반?
        while(ban <= K){
            int student = sc.nextInt(); // 학생 수
            int [] mathScore = new int [student]; // 수학 점수 배열

            for(int i=0; i<student; i++) // 수학 점수 입력
                mathScore[i] = sc.nextInt();

            Arrays.sort(mathScore); // 내림차순으로 정렬
            // 가장 큰 인접한 점수 차이 구하기
            int maxSub = Math.abs(mathScore[0]-mathScore[1]);
            for(int i=1; i<student-1; i++){
                if(maxSub < Math.abs(mathScore[i]-mathScore[i+1]))
                    maxSub = Math.abs(mathScore[i]-mathScore[i+1]);
            }
            // 예제 처럼 출력
            System.out.println("Class "+ ban);
            System.out.println("Max "+mathScore[mathScore.length-1]
            +", Min "+mathScore[0]+", Largest gap "+maxSub);
            ban++; // 다음 class
        }
    }
}