代码改变世界

华为招聘机试整理13:算分数的问题

2017-04-25 21:01  tlnshuju  阅读(616)  评论(0编辑  收藏  举报

华为招聘机试整理13:算分数的问题

题目:算分数的问题
算分数的问题。去掉一个最高分一个最低分。之后求平均
题目分析:
直接利用#include<algorithm>中的sort排序。之后直接去除第一个和最后一个就能够了。最后精度考虑吧。我们这里用#include<iomanip>中的setiosflags(ios::fixed) << setpresicion(4)

==========================================================================
參考代码:

//算分数的问题.cpp
//2014.7.11 hepanhui
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;

float avescore(float score[],int n)
{
    float sumscore = 0.0;
    float avescore = 0.0; 
    sort(score, score + n);
    for(int i = 1; i < n-1; i++)
    {
        sumscore += score[i]; 
    }
    avescore = sumscore/(n - 2);
    return avescore;
}

int main()
{
    float score[6]={70,80,90,98,87,86}; 
    cout << setiosflags(ios::fixed) << setprecision(4) << avescore(score, 6) << endl;
    return 0;
}