본문 바로가기
coding/algorithm

[SW Expert Academy] D1 2071. 평균값 구하기(c++)

by 눈부신음표 2022. 5. 20.
728x90

*cmath -> round(숫자) / return값 : 실수*

 

더보기
#include <iostream>
#include <cmath>

using namespace std;

int main(){
    int tc, num;
    cin >> tc;
    for(int i=0; i<tc; i++){
        int result = 0;
        for(int j=0; j<10; j++){
            cin >> num;
            result += num;
        }
        cout<<"#"<<i+1<<" "<<(int)round((double)result/10)<<endl;
    }
}

문제

https://swexpertacademy.com/main/code/problem/problemDetail.do 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

728x90

[접근]

result에 숫자를 더해주고 마지막에 10으로 나눈다.

반올림한 값을 구하라고 했으므로, cmath 라이브러리 안의 round(숫자) 함수를 사용한다.

return 값이 실수이므로 integer로 변환시켜 출력한다.

int result = 0;
for(int j=0; j<10; j++){
    cin >> num;
    result += num;
}
cout<<"#"<<i+1<<" "<<(int)round((double)result/10)<<endl;
728x90

댓글