반응형
문제 링크
코드
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed;
cout.precision(3);
int n, i, student, s, sum, cnt;
double avg, rate;
cin >> n;
while (n--) {
cin >> student;
sum = 0, cnt = 0;
vector<int> score;
for (i = 0; i < student; i++) {
cin >> s;
score.push_back(s);
sum += s;
}
avg = (double)sum / student;
for (i = 0; i < student; i++)
if (score[i] > avg) cnt++;
rate = (double)cnt / student;
cout << rate * 100 << "%\n";
}
}
설명
나눗셈을 할 때 몫만 나오는 것을 방지하기 위해서 int형 앞에 (double)을 붙이거나 1.0을 곱해주는 것을 유의하면 좋을 거 같다.
(주의) 기록용으로 작성한 글입니다. 좋은 코드가 아닐 수 있습니다.
댓글 환영합니다!
반응형
'Algorithm > BOJ' 카테고리의 다른 글
[백준/BOJ] 4963번 - 섬의 개수 (C++) (0) | 2022.03.24 |
---|---|
[백준/BOJ] 4949번 - 균형잡힌 세상 (C++) (0) | 2022.03.24 |
[백준/BOJ] 3135번 - 라디오 (C++) (0) | 2022.03.23 |
[백준/BOJ] 3273번 - 두 수의 합 (C++) (0) | 2022.03.23 |
[백준/BOJ] 3059번 - 등장하지 않는 문자의 합 (C++) (0) | 2022.03.23 |
댓글