,一道华为下机题
求助,一道华为上机题
题目描述: 选秀节目打分,分为专家评委和大众评委,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)
{
int n1=0;
int n2=0;
int sum1=0,sum2=0;
int avarage=0;
for(int i=0;i<n;i++)
{
if(1==judge_type[i])
n1++;
else
n2++;
}
if(0==n2)
{
for(int j=0;j<n;j++)
{
sum1+=score[j];
}
avarage=sum1/n;
return avarage;
}
else if(n2!=0)
{
for(int k=0;k<n;k++)
{
if(1==judge_type[k])
sum1+=judge_type[k];
else
sum2+=judge_type[k];
}
assert(n1 !=0);
avarage=(sum1/n1)*0.6+(sum2/n2)*0.4;
return avarage;
}
}
void main()
{
int score[10]={1,2,3,5,6,7,8,9,4,7};
int judge_type[10]={2,2,2,1,2,2,2,2,2,2};
int n=10;
int avar=cal_score(score,judge_type,10);
cout<<avar<<endl;
}这样可以吗?请求帮助
------解决方案--------------------
我没看你的程序,就看了题目,自己试着在纸上写了一下,然后和你的一对,就发觉差别了。先不说正确不正确,只谈效率问题。
那个功能函数中,其实只需要完整的执行一次for循环就能得出结果了,你用了3个for循环,而且代码可以精简很多,效率也能高很多
------解决方案--------------------
int强制转换是舍弃小数,而题中的取整的意思,我觉得应该是按照四舍五入。
------解决方案--------------------
题目描述: 选秀节目打分,分为专家评委和大众评委,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)
{
int n1=0;
int n2=0;
int sum1=0,sum2=0;
int avarage=0;
for(int i=0;i<n;i++)
{
if(1==judge_type[i])
n1++;
else
n2++;
}
if(0==n2)
{
for(int j=0;j<n;j++)
{
sum1+=score[j];
}
avarage=sum1/n;
return avarage;
}
else if(n2!=0)
{
for(int k=0;k<n;k++)
{
if(1==judge_type[k])
sum1+=judge_type[k];
else
sum2+=judge_type[k];
}
assert(n1 !=0);
avarage=(sum1/n1)*0.6+(sum2/n2)*0.4;
return avarage;
}
}
void main()
{
int score[10]={1,2,3,5,6,7,8,9,4,7};
int judge_type[10]={2,2,2,1,2,2,2,2,2,2};
int n=10;
int avar=cal_score(score,judge_type,10);
cout<<avar<<endl;
}这样可以吗?请求帮助
------解决方案--------------------
我没看你的程序,就看了题目,自己试着在纸上写了一下,然后和你的一对,就发觉差别了。先不说正确不正确,只谈效率问题。
那个功能函数中,其实只需要完整的执行一次for循环就能得出结果了,你用了3个for循环,而且代码可以精简很多,效率也能高很多
------解决方案--------------------
int强制转换是舍弃小数,而题中的取整的意思,我觉得应该是按照四舍五入。
------解决方案--------------------
- C/C++ code
int CallScore(int N,int *Score,int *Judge_type) { int ret=0,n=0,m=0; double sum1=0,sum2=0; if(n&&Score&&Judge_type){ for(int i=0;i<N;++i) switch(Judge_type[i]){ case 1: sum1 += Score[i];++n;break; case 2: sum2 += score[i];++m;break; default:;//----舍弃不符要求数据 } if(n)sum1=int(sum1 / n); if(m)sum2 = int (sum2/m); ret = m?sum1*0.6+sum2*0.4:sum1; } return ret; }
------解决方案--------------------
先写为double,再强制转换为int吧,否则结果可能不正确