班上有學生若干名,已知每名學生的成績(整數),求班上所有學生的平均成績,保留到小數點后兩位。同時輸出該平均成績整數部分四舍五入后的數值。 第一行有一個整數n(1<= n <= 100),表示學生的人數。其后n行每行有1個整數,表示每個學生的成績,取值在int范圍內。


#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 ;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM