근본없는 코딩
[C++] 백준4344 평균은 넘겠지 본문

#include <iostream>
using namespace std;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int c, n, score[1001];
	int sum, goodScoreCnt;
	double avg;
	cout << fixed;
	cout.precision(3);
	cin >> c;
	for (int i = 0; i < c; i++) {
		cin >> n;
		sum = 0;
		goodScoreCnt = 0;
		for (int j = 0; j < n; j++) {
			cin >> score[j];
			sum += score[j];
		}
		avg = (double)sum / n;
		for (int j = 0; j < n; j++) {
			if (score[j] > avg) goodScoreCnt++;
		}
		cout << (double)goodScoreCnt / n * 100 << '%' << '\n';
	}
	return 0;
}
✔️ 소수점 자리수
cout을 사용하여 실수를 출력하면 전체 자리수(정수 부분 + 소수점 부분)가 6자리로 고정되어 출력된다.
전체 자리수를 수정하는 방법은 다음과 같다.
cout<<fixed;
cout.precision(number);cout.precision(number); 여기서 number 자리에 출력하고 싶은 소수점의 자리수를 작성하면 된다.
'✔ Online Judge' 카테고리의 다른 글
| [C++] 백준1316 그룹 단어 체커 (0) | 2023.06.29 | 
|---|---|
| [C++] 백준2941 크로아티아 알파벳 (0) | 2023.06.29 | 
| [C++] 백준1157 단어공부 (0) | 2023.06.29 | 
| [C++] 백준10988 팰린드롬인지 확인하기 (0) | 2023.06.29 | 
| [C++] 백준10812 바구니 순서 바꾸기 (0) | 2023.06.29 | 
 
                   
                   
                  