[ 백준 1546번 ] 평균

2021. 11. 12. 23:38백준/단계별 문제

백준 1546번 / 5단계 / 평균

 

백준 1545번 - 평균

 

채점결과는 다음과 같다

  메모리 시간
1번째 방법 2020 KB 0 ms

 

문제 풀이
1. 시험과목 수와 시험 과목당 점수를 입력받으면서 max함수를 이용해 최고점수를 score_max변수에 넣는다.

과목점수: 40 80 60
최고점: 80
score_max = 80

2. 반복문을 돌려 현재점수 = 현재점수 = 현재점수 / max * 100 처리를 해준뒤 합계변수 sum에 넣는다

score[0] = 40
40 / 80 * 100 = 50
score[0] = 50
sum = sum + 50

3. 반복문이 끝난 뒤 sum 의 평균을 구해준다.

sum = 50 + 100 + 75
평균 = sum / 3
평균 = 75

 

코드
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
	int n, i;
	double score[1000];
	double score_max = 0;
	double sum = 0;

	scanf("%d", &n);

	for ( i = 0; i < n; i++ ) {
		cin >> score[i];
		score_max = max(score_max, score[i]);
	}

	for ( i = 0; i < n; i++ ) {
		score[i] = score[i] / score_max * 100;
		sum += score[i];
	}

	sum /= n;
	cout << sum;
}

'백준 > 단계별 문제' 카테고리의 다른 글

[ 백준 4344번 ] 평균은 넘겠지  (0) 2021.11.22
[ 백준 8958번] OX퀴즈  (0) 2021.11.15
[ 백준 3052번 ] 나머지  (0) 2021.11.12
[ 백준 2562번 ] 최댓값  (0) 2021.11.09
[ 백준 10818번 ] 최소, 최대  (0) 2021.11.09