Algorithm

[Algorithm] Softeer : 성적 평균

kimsangjunzzang 2024. 11. 27. 09:45
728x90
반응형
#include<iostream>
#include<vector>
#include<cmath>

using namespace std;

int main(int argc, char** argv)
{
    int num,testCase; cin >> num >> testCase;
    vector<int>v(num);
    for ( int i=0;i<num;i++) {
        cin>>v[i];
    }
    for( int i =0;i<testCase;i++) {
        int a,b; cin >> a >> b;
        int sum = 0;
        for ( int j=a;j<=b;j++) {
            sum +=v[j-1];
        }
        cout<<fixed;
        cout.precision(2);
        cout << sum / float(b-a+1) << endl;
    }

   return 0;
}

 

소수점 셋째 자리에서 반올림을 진행하기 때문에 소수점을 고정 할 필요가 있습니다.

  • fixed는 소수점 아래 이후부터 고정시키겠다는 표현입니다.
  • cout.precision를 호출하여 출력되는 숫자의 자릿수를 결정하게 됩니다.
  • fixed를 사용하지 않으면, 전체 모든 숫자의 갯수로 설정하게 되고, fixed를 사용하면 소수점 이후부터 자릿수를 설정하게 됩니다
728x90
반응형