문제 풀이 및 개발 공간

[백준] 5800번 성적 통계 문제! (silver 5 본문

백준공부/java

[백준] 5800번 성적 통계 문제! (silver 5

gomduri43 2022. 8. 18. 09:07

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;
		int k=Integer.parseInt(br.readLine());
		int[] score;
		int max=0;
		for(int i=0; i<k; i++) {
			st=new StringTokenizer(br.readLine());
			int n=Integer.parseInt(st.nextToken());
			score=new int[n];
			for(int j=0; j<n; j++) {
				score[j]=Integer.parseInt(st.nextToken());
			}
			score=bubbleSort(score,score.length);
			max=maxMinus(score);
			bw.write("Class "+(i+1)+"\n");
			bw.write("Max "+score[n-1]+", Min "+score[0]+", Largest gap "+max+"\n");
		}
		bw.flush();
		
		
	}
	public static int[] bubbleSort(int[] score, int length) {
		int temp;
		for(int i=length-1; i>0; i--) {
			for(int j=0; j<i; j++) {
				if(score[j]>score[j+1]) {
					temp=score[j+1];
					score[j+1]=score[j];
					score[j]=temp;
				}
			}
		}
		return score;
	}
	public static int maxMinus(int[] score) {
		int max=0;
		for(int i=0; i<score.length-1; i++) {
			if(score[i+1]-score[i]>max) {
				max=score[i+1]-score[i];
			}
		}
		return max;
	}
}