mulll

[소프티어] 성적 평균 / C++ 해설 본문

algorithm study

[소프티어] 성적 평균 / C++ 해설

dongha 2022. 12. 27. 01:15

더 많은 문제풀이는 아래 Github 주소에서 확인하실 수 있습니다.

https://github.com/Dongha-k/softeer-code

 

GitHub - Dongha-k/softeer-code: softeer 문제 풀이입니다.

softeer 문제 풀이입니다. Contribute to Dongha-k/softeer-code development by creating an account on GitHub.

github.com

문제 출처: https://softeer.ai/practice/info.do?idx=1&eid=389 

 

Softeer

연습문제를 담을 Set을 선택해주세요. 취소 확인

softeer.ai

 

 

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

int main(int argc, char** argv)
{
	int n, q;
	vector<int> score;
	cin >> n >> q;
	score = vector<int>(n + 1, 0);

	for(int i = 1 ; i <= n ; i ++) {
		cin >> score[i];
		score[i] += score[i - 1];
	}

	
	while(q--){
		int a, b;
		cin >> a >> b;
		a--;
		printf("%.2f\n", (score[b] - score[a]) / (double)(b - a));
	}
	return 0;
}

 

1. 구간합을 이용한 문제풀이

 

2. C++의 Cout 대신 cstdio를 import하여 소수점 처리

Comments