/********************************************************************************************
9月7日,一道华为上机题:
题目描述: 选秀节目打分,分为专家评委和大众评委,score[] 数组里面存
储每个评委打的分数,judge_type[] 里存储与score[] 数组对应的评委类
别,judge_type == 1,表示专家评委,judge_type == 2,表示大众评委,
n表示评委总数。打分规则如下:专家评委和大众评委的分数先分别取
一个平均分(平均分取整),然后,总分 = 专家评委平均分 * 0.6 +
大众评委 * 0.4,总分取整。如果没有大众评委,则 总分 = 专家评委平均分,
总分取整。函数最终返回选手得分。
函数接口 int cal_score(int score[], int judge_type[], int n)
上机题目需要将函数验证,但是题目中默认专家评委的个数不能为零,
但是如何将这种专家数目为0的情形排除出去。
*******************************************************************************************/
#include <iostream>
#include <assert.h>
using namespace std;
int cal_score(int score[], int judge_type[], int n)
{
assert(n>0);
int specialistScore = 0;
int specialistNum = 0;
int popularScore = 0;
int popularNum = 0;
for (int i=0; i<n; ++i) {
if (judge_type[i]==1) {
++specialistNum;
specialistScore += score[i];
} else if (judge_type[i]==2) {
++popularNum;
popularScore += score[i];
}
}
if (specialistNum && popularNum) {
return int(int(double(specialistScore)/specialistNum+0.5)*0.6+int(double(popularScore)/popularNum+0.5)*0.4+0.5);
} else if (specialistNum && !popularNum)
return int(double(specialistScore)/specialistNum+0.5);
else
return 0;//无专家的情况
}
int main(int argc, char **argv)
{
int score[10]={1,2,3,5,6,7,8,9,4,7};
int judge_type[10]={1,1,1,1,1,1,1,1,1,1};
int n=10;
int avar=cal_score(score,judge_type,10);
cout<<avar<<endl;
system("pause");
return 0;
}
水一题~
一道华为上机题
最新推荐文章于 2022-12-06 20:59:41 发布