[SWEA] D1.평균값 구하기 (C++)

jhyunn·2024년 2월 24일
0

SWEA

목록 보기
2/12

2071. 평균값 구하기

문제 링크

#include <iostream>
 
using namespace std;
 
int main() {
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        float ans = 0;
        for (int i = 0; i < 10; i++) {
            int n;
            cin >> n;
            ans += n;
        }
        ans /= 10;
        if (ans - int(ans) >= 0.5) ans++;
     
        cout << "#" << t << " " << int(ans) << endl;
    }
}

 
풀이
cin은 공백을 기준으로 입력받기 때문에 하나씩 받을 수 있다.
주어지는 수가 10개라는 정보가 주어졌기 때문에 10번만 loop하며 구한다.
반올림의 경우, 소수 첫째 자리 기준이기 때문에 0.5를 기준으로 조정한다.

profile
https://github.com/Sungjeonghyun

0개의 댓글