근본없는 코딩

[C++] 백준4344 평균은 넘겠지 본문

✔ Online Judge

[C++] 백준4344 평균은 넘겠지

근본없는 개발자 2023. 6. 29. 23:54

#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 자리에 출력하고 싶은 소수점의 자리수를 작성하면 된다.