4344번 - 평균은 넘겠지

2022. 4. 26. 16:19백준/C++

문제

대학생 새내기들의 90%는 자신이 반에서 평균은 넘는다고 생각한다. 당신은 그들에게 슬픈 진실을 알려줘야 한다.

입력

첫째 줄에는 테스트 케이스의 개수 C가 주어진다.

둘째 줄부터 각 테스트 케이스마다 학생의 수 N(1 ≤ N ≤ 1000, N은 정수)이 첫 수로 주어지고, 이어서 N명의 점수가 주어진다. 점수는 0보다 크거나 같고, 100보다 작거나 같은 정수이다.

출력

각 케이스마다 한 줄씩 평균을 넘는 학생들의 비율을 반올림하여 소수점 셋째 자리까지 출력한다.

 

 

Tip. int / int  -> float하면 답이 이상하게 나오기 때문에 

int중 하나를 float으로 바꿔준다. 

#include <iostream>

using namespace std;

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

    int repeat;
    cin>>repeat;

    while(repeat--)
    {
        int studentnum;
        cin>>studentnum;
        int score[studentnum];
        for(int i = 0; i<studentnum; i++)
        {
            cin>>score[i];
        }

        float average=0;
        for(int i = 0; i<studentnum; i++)
            average +=  score[i];
        average /= studentnum;
        
        float averStudent = 0;
        for(int i = 0; i<studentnum; i++)
        {
            if(average<score[i])
                averStudent++;
        }
        float averStudentP = averStudent/studentnum*100;
        
        cout.precision(3);
        cout<<fixed;
        cout<<averStudentP<<"%"<<"\n";
    }
    return 0;
}

'백준 > C++' 카테고리의 다른 글

2164번 - 카드2  (0) 2022.05.01
18258번 - 큐2  (0) 2022.05.01
8958번 - OX퀴즈  (0) 2022.04.26
1546번 - 평균  (0) 2022.04.26
3052번 - 나머지  (0) 2022.04.26