班上有學生若干名,已知每名學生的成績(整數),求班上所有學生的平均成績,保留到小數點後兩位。同時輸出該平均成績整數部分四捨五入後的數值。 第一行有一個整數n(1<= n <= 100),表示學生的人數。其後n行每行有1個整數,表示每個學生的成績,取值在int范圍內。,excel小數點四捨五入
#include<iostream>
#include<iomanip>
using namespace std ;
int main()
{
int n;
while(cin>>n)
{
int score, score_sum = 0;
for(int i=0;i<n;i++)
{
cin>>score ;
score_sum+=score;
}
double ave_score = (score_sum * 1.0) / n ;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<ave_score<<" ";
int ave = (int)(ave_score) ; // 雙精度型強制轉化為整形
ave = ( (ave + 5) / 10 ) * 10 ; //對整形進行四捨五入
cout << ave << endl ;
}
return 0 ;
}