47

问答题 47 /49

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

参考答案

#include "iostream"
using namespace std;
int cal_score(int score[], int judge_type[], int n)
{
    if (NULL == score || NULL == judge_type || 0 == n)  return 0;
    int sum = 0;
    int sum1 = 0, count1 = 0;
    int sum2 = 0, count2 = 0;
    for (int i = 0; i < n; i++)
    {
        if (judge_type[i] == 1)
        {
            sum1 = sum1 + score[i];
            count1++;
        }
        else
        {
            sum2 = sum2 + score[i];
            count2++;
        }
    }
    if (0 == count2)  sum = sum1 / count1;
    else  sum = (sum1 / count1) * 0.6 + (sum2 / count2) * 0.4;
    return sum;
}
void main()
{
    int score[3] = {12, 13, 15};
    int judge_type[3] = {1, 1, 2};
    printf("%d\n", cal_score(score, judge_type, 3) );
}