Programming/Baekjoon

백준 5800 성적 통계 [c++]

fishersheep 2022. 1. 17. 15:47
반응형
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
using namespace std;

bool cmp(int a, int b)
{
	return b < a;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int k;	//반의수
	int n;	//각반의 학생의수
	int max[101];	//최댓값 배열
	int min[101];	//최솟값 배열
	int gap[101];	//가장큰 인접한 점수차이 배열

	int g=0;	//점수차이 비교를 위한 변수

	cin >> k;

	for (int i = 0; i < k; i++)	//반의 수만큼 반복
	{
		cin >> n;	//학생의 수 입력
		int *arr = new int[n];	
		for (int j = 0; j < n; j++)
		{
			cin >> arr[j];	//성적을 배열로 입력
		}
		sort(arr, arr + n);	//오름차순 정렬
		max[i] = arr[n - 1];	//최댓값 배열에 저장
		sort(arr, arr + n,cmp);	//내림차순 정렬
		min[i] = arr[n - 1];	//최솟값 배열에 저장

		for (int l = 0; l < n-1; l++)	//내림차순으로 정렬된 배열에서 인접한수의 차이를 구하는 반복문
		{
			if (g < arr[l] - arr[l + 1])
			{
				g = arr[l] - arr[l + 1];
				gap[i] = g;
			}
		}
		g = 0;	//초기화
		delete[] arr;
	}

	for (int i = 0; i < k; i++)	//결과출력
	{
		cout << "Class " << i+1 << '\n';
		cout << "Max " << max[i] << ", " << "Min " << min[i] << ", " << "Largest gap " << gap[i] << '\n';
	}


	
	return 0;
}
반응형

'Programming > Baekjoon' 카테고리의 다른 글

백준 11170 c++  (0) 2022.01.18
백준 11047 동전0 [c++]  (0) 2022.01.17
백준 10773 제로 [c++]  (0) 2022.01.17
백준 1427 소트인사이드 [c++]  (0) 2022.01.17
백준 1037 약수 [c++]  (0) 2022.01.17