题解 | 牛牛的数学作业

牛牛的数学作业

https://www.nowcoder.com/practice/6f5d239c34a7429cb325a3d836abc342

#include <iomanip>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

void solve();
int main() {
    int T;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}
void solve() {
    long long n;
    vector<long long> a;    
    cin>>n;
    long long sum=0;
    for(int j=0;j<n;j++){
        int x;
        cin>>x;
        a.push_back(x);
        sum+=a[j];
    }
    sort(a.begin(),a.end());
    int range = a[n-1]-a[0];
    double avarge = static_cast<double>(sum)/n ;
    double variance =0.0;
    for(int j=0;j<n;j++){
        variance += pow(a[j]-avarge,2); 
    }
    variance /= n;
    cout<<range<<" "<<fixed<<setprecision(3)<<variance<<endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

有气魄的马来熊在摸鱼:我爱vivo 马上换手机 vivo我爱你!!!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务