• 华为机试


    选秀节目评分 选秀节目打分,分为专家评委和大众评委,score[]数组里面存储每个评委打的分数,judge_type[] 里存储与 score[] 数组对应的评委类别,judge_type[i] == 1,表示专家评委 judge_type[i] == 2,表示大众评委,n表示评委总数。 打分规则如下: 专家评委和大众评委的分数先分别取一个平均分(平均分取整),然后,总分 = 专家评委平均分*0.6 + 大众评委*0.4,总分取整。 如果没有大众评委,则 总分 = 专家评委平均分,总分取整。函数最终返回选手得分。

    #include<iostream>
    using namespace std;
    //算法一(这个考虑更全面)
    int cal_score(int score[], int judge[], int N)
    {
        int ret = 0, n = 0, m = 0;
        double sum1 = 0, sum2 = 0;
        if (N&&score&&judge)
        {
            for (int i = 0; i < N;i++)
              switch (judge[i])
             {
              case 1:sum1 += score[i]; ++n; break;
              case 2:sum2 += score[i]; ++m; break;
              default:;
              }
            if (n) sum1 = int(sum1 / n);//考虑到专家人数为0,务必确保除数不为0
            if (m) sum2 = int(sum2 / m);
            ret = m ? sum1*0.6 + sum2*0.4 : sum1;
        }
        return ret;
    }
    //算法二
    int cal_score1(int score[], int judge_type[], int n)
    {
        int scr = 0;
        int sum1 = 0.0;
        int sum2 = 0.0;
        int cnt = 0;//专家评委.  
        for (int i = 0; i < n; i++)
        {
            if (judge_type[i] == 1)
            {
                cnt++;
                sum1 += score[i];
            }
            else if (judge_type[i] == 2)
                sum2 += score[i];
        }
        if (cnt == n)
            scr = sum1 / cnt;
        scr = 0.6*(sum1 / cnt) + 0.4*(sum2 / (n - cnt));
        return scr;
    }
    int main()
    {
        int n = 10;
        int score[10] = { 80, 85, 90, 80, 75, 95, 80, 90, 95, 92 };
        int judge[10] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 2 };
        int final_score = cal_score(score, judge, n);
        cout << final_score << endl;
        system("pause");
        return 0;
    }
  • 相关阅读:
    uva 10986
    sed命令详解及应用实例
    JavaScript弹出层
    eclipspe导入hibernate的源代码
    用eclipse查看JDK源代码
    Java设计模式之适配器模式(Adapter)
    Java计算程序运行时间
    Java的IO流各个类的使用原则
    Java的IO输入输出流类的介绍(有图)
    解惑:字、位、字节、字符、字符串。
  • 原文地址:https://www.cnblogs.com/wft1990/p/6545611.html
Copyright © 2020-2023  润新知