[백준 5800] 성적 통계 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
여러 학급의 점수 데이터에 대해 최댓값, 최솟값, 가장 큰 점수 차이를 구하는 문제입니다.
입력은 여러 줄로 구성되며, 각 줄은 한 학급의 점수 데이터를 나타냅니다.
출력은 각 학급마다 Class X
라벨과 함께 최댓값, 최솟값, 최대 점수 차이를 순서대로 출력합니다.
접근법
- 각 테스트 케이스에서 점수를 벡터에 저장한 뒤 오름차순 정렬합니다.
- 정렬된 리스트에서:
- 최댓값은 마지막 원소
- 최솟값은 첫 번째 원소
- 최대 점수 간격은 인접한 원소 간의 차이 중 최대값
- 각 학급에 대해 라벨링된 출력 포맷을 그대로 맞춰 출력합니다.
Code
[ C# ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Linq;
namespace Solution {
class Program {
static void Main(string[] args) {
int numClass = int.Parse(Console.ReadLine()!);
for (int classNum = 1; classNum <= numClass; classNum++) {
var scores = Console.ReadLine()!.Split().Select(int.Parse).ToArray();
int numS = scores[0];
var arr = scores.Skip(1).ToArray();
Array.Sort(arr);
int maxGap = 0;
for (int i = 1; i < numS; i++)
maxGap = Math.Max(maxGap, arr[i] - arr[i - 1]);
Console.WriteLine("Class " + classNum);
Console.WriteLine($"Max {arr[numS - 1]}, Min {arr[0]}, Largest gap {maxGap}");
}
}
}
}
[ C++ ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int numClass; cin >> numClass;
int cntClass = 1;
while (numClass--) {
int numS; cin >> numS;
vi score(numS);
for (int i = 0; i < numS; i++)
cin >> score[i];
sort(score.begin(), score.end());
int maxGap = 0;
for (int i = 0; i < numS - 1; i++) {
int gap = score[i + 1] - score[i];
if (gap > maxGap) maxGap = gap;
}
cout << "Class " << cntClass << "\n";
cout << "Max " << score[numS - 1];
cout << ", Min " << score[0];
cout << ", Largest gap " << maxGap << "\n";
cntClass++;
}
return 0;
}