[백준] 4344 - 평균은 넘겠지

hhjj0506·2021년 9월 11일
0

알고리즘

목록 보기
1/3

문제

It is said that 90% of frosh expect to be above average in their class. You are to provide a reality check.

The first line of standard input contains an integer C, the number of test cases. C data sets follow. Each data set begins with an integer, N, the number of people in the class (1 <= N <= 1000). N integers follow, separated by spaces, each giving the final grade (an integer between 0 and 100) of a student in the class.

For each case you are to output a line giving the percentage of students whose grade is above average, rounded to 3 decimal places.

풀이

문제 자체는 그렇게 어렵지 않았다. Test case의 숫자를 입력받아 그 숫자만큼 실행하는 for문 속에서 각 반마다의 시험 성적을 받는 것 하나, 그리고 평균 점수를 구한 후 각 시험 점수들과 비교해 평균 초과인 사람의 숫자를 찾는 반복문들을 사용했다.
백분율로 변환할 때는 (x / y) * 100을 이용해 변환했는데 이때 y를 float로 변환하여 계산하지 않았던걸 몰라서 조금 시간이 걸렸다.
분명히 좀 더 효율적이고 쉽게 푸는 방법이 있을 것 같지만 아직 내 실력으로는 모르겠다.

코드

#include <iostream>
using namespace std;

int main()
{
    int c, n, *arr, ave = 0;
    float *res, temp = 0;
    
    cin >> c;
    res = new float[c];
    
    for (int i = 0; i < c; i++) {
        cin >> n;
        arr = new int[n];
        
        for (int j = 0; j < n; j++) {
            cin >> arr[j];
            ave = ave + arr[j];
        }
        ave = ave / n;
        // 학생들과 평균 점수 비교, 초과하면 포함
        // 백분율로 평균 이상을 받은 학생의 비율 나타내기
        // (x / y) * 100 = percentage
        for (int l = 0; l < n; l++) {
            if (arr[l] > ave) temp++;
        }
        res[i] = (temp / (float)n) * 100;
        ave = 0;
        temp = 0;
    }
    
    for (int k = 0; k < c; k++) {
        printf("%.3f%\n", res[k]);
    }
    

    return 0;
}
profile
눈부시게 높은 하늘 그보다 더 큰 꿈을 꿔

0개의 댓글