코딩/백준 문제 (브론즈)

[백준/4344/C언어] 평균은 넘겠지 _ 풀이

룻밤 2023. 8. 4. 19:22

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

 

4344번: 평균은 넘겠지

각 케이스마다 한 줄씩 평균을 넘는 학생들의 비율을 반올림하여 소수점 셋째 자리까지 출력한다. 정답과 출력값의 절대/상대 오차는 10-3이하이면 정답이다.

www.acmicpc.net


풀이

#include <stdio.h>

int main() {
	int c, n;		// c=테스트케이스, n= 케이스별 학생수
	int score[1000];	// 학생수에 따른 점수
	int total = 0;	// total += 성적
	int avg;		// 평균
	int cnt = 0;	// 평균보다 높은 점수
	double avrr[100];	// 케이스별 비율

	scanf("%d", &c);
	for (int i = 0; i < c; i++) {		// c만큼 반복
		scanf("%d", &n);
		for (int j = 0; j < n; j++) {	// 학생수만큼
			scanf("%d", &score[j]);
			total += score[j];			// 성적 더하기
		}
		avg = total / n;	// 평균
		for (int j = 0; j < n; j++) {
			if (score[j] > avg) {	// 평균보다 점수가 높을때
				cnt++;		// cnt에 저장
			}
		}
		avrr[i] = (double)cnt / n;	// 출력할 비율 계산
		cnt = 0;	// 초기화
		total = 0;	// 초기화
		avg = 0;	// 초기화
	}
	
	for (int i = 0; i < c; i++)
		printf("%.3lf%%\n", avrr[i]*100);	// 소수점세번째자리까지 출력
	return 0;
}