求助,一道华为上机题

nunumi 2012-09-07 03:38:08
题目描述: 选秀节目打分,分为专家评委和大众评委,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;
}这样可以吗?请求帮助
...全文
5699 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
六公子 2012-11-27
  • 打赏
  • 举报
回复
楼主的代码执行效率很高的!
引用 13 楼 FreeFice 的回复:
C/C++ code? 1234567891011121314151617 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 ……
z
andyliuxs 2012-10-07
  • 打赏
  • 举报
回复
华为上机题目都是很简单的,但是考得就是考虑问题全面与否。这个地方一定要考虑溢出问题,所以在求和的时候不能用int而是long。
CZQing_ze 2012-09-17
  • 打赏
  • 举报
回复
感觉华为的上机题注重分析问题全不全面
questl 2012-09-14
  • 打赏
  • 举报
回复
int CallScore(int N,int *Score,int *Judge_type)
{
int ret=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];break;
case 2: sum2 += score[i];break;
default:;//----舍弃不符要求数据
}

ret = sum2==0?-1:(sum1*0.6+sum2*0.4)/N;
}
return ret;
}

如果是0的话,计数不是必须的吧;
滴答滴答D 2012-09-13
  • 打赏
  • 举报
回复

int cal_score(const int score[], const int judge_type[], int n)
{
int expertN=0, expertS=0;
int otherN=0, otherS=0;
int i;
for(i=0; i<n; i++)
{
if(1==judge_type[i])
{
expertN++;
expertS += score[i];
continue;
}
else if(2==judge_type[i])
{
otherN++;
otherS += score[i];
continue;
}
}

if(0==otherN)
{
return expertS/expertN;
}
else
{
return expertS/expertN*0.6 + otherS/otherN*0.4;
}
}

顺便写写,忘指教。
ParkThuya 2012-09-13
  • 打赏
  • 举报
回复
++judge_expert_avg_score;
++judge_public_avg_score;
这里都写错了,名字也不对啊~~
凡玛多 2012-09-13
  • 打赏
  • 举报
回复
都是牛人啊,看不懂,只是来瞧瞧。
xiaohu023 2012-09-13
  • 打赏
  • 举报
回复
judge_expert_avg_score/judge_expert_num
是我的话,我会这样写,
((float)judge_expert_avg_score)/judge_expert_num
gaochizhen33 2012-09-13
  • 打赏
  • 举报
回复

int cal_score(int score[], int judge_type[], int n)
{
double sum1 = 0; //专家总分
double sum2 = 0; //大众总分
int i = 0, num1 = 0, num2 = 0;
for(; i<n; i++)
{
switch(judge_type[i])
{
case 1:
sum1 += score[i];
num1++; //专家个数
break;
case 2:
sum2 += score[i];
num2++; //大众个数
break;
default:
break;
}
}
return num2?(int)(sum1/num1*0.6+sum2/num2*0.4):(int)(sum1/num1);
}

怎么没发上去。。按照楼主说的由测试者保证必须有专家评分 就没判断专家为0的情况了
xiaohu023 2012-09-13
  • 打赏
  • 举报
回复
高人~~
[Quote=引用 27 楼 的回复:]

C/C++ code

typedef enum
{
JUDGE_EXPERT = 1,
JUDGE_PUBLIC = 2
} judge_t;

#define JUDGE_EXPERT_FACTOR (0.6)
#define JUDGE_PUBLIC_FACTOR (0.4)

int calculate_score(int judge_score[], judge_t ……
[/Quote]
xiaohu023 2012-09-13
  • 打赏
  • 举报
回复
高人~~
[Quote=引用 27 楼 的回复:]

C/C++ code

typedef enum
{
JUDGE_EXPERT = 1,
JUDGE_PUBLIC = 2
} judge_t;

#define JUDGE_EXPERT_FACTOR (0.6)
#define JUDGE_PUBLIC_FACTOR (0.4)

int calculate_score(int judge_score[], judge_t ……
[/Quote]
gaochizhen33 2012-09-13
  • 打赏
  • 举报
回复

int cal_score(int score[], int judge_type[], int n)
{
double sum1 = 0; //专家总分
double sum2 = 0; //大众总分
int i = 0, num1 = 0, num2 = 0;
for(; i<n; i++)
{
switch(judge_type[i])
{
case 1:
sum1 += score[i];
num1++; //专家个数
break;
case 2:
sum2 += score[i];
num2++; //大众个数
break;
default:
break;
}
}
return num2?(int)(sum1/num1*0.6+sum2/num2*0.4):(int)(sum1/num1);
}

水上一贴。。
ParkThuya 2012-09-13
  • 打赏
  • 举报
回复

typedef enum
{
JUDGE_EXPERT = 1,
JUDGE_PUBLIC = 2
} judge_t;

#define JUDGE_EXPERT_FACTOR (0.6)
#define JUDGE_PUBLIC_FACTOR (0.4)

int calculate_score(int judge_score[], judge_t judge_type[], int judge_num)
{
int score = 0;
int judge_expert_avg_score = 0, judge_public_avg_score = 0;
int judge_expert_num = 0, judge_public_num = 0;

for (int i = 0; i < judge_num, ++i)
{
if (judge_type[i] == JUDGE_EXPERT)
{
judge_expert_avg_score += judge_score[i];
++judge_expert_avg_score;
}
else if (judge_type[i] == JUDGE_PUBLIC)
{
judge_public_avg_score += judge_score[i];
++judge_public_avg_score;
}
}

if (judge_expert_num != 0)
score += judge_expert_avg_score/judge_expert_num * JUDGE_EXPERT_FACTOR;
if (judge_public_num != 0)
score += judge_public_avg_score/judge_public_num * JUDGE_PUBLIC_FACTOR;

return score;
}
nij6173 2012-09-12
  • 打赏
  • 举报
回复
#cal_score.py
def cal_score(score = [],judge = []):
score = input('Enter score: ')
judge = input('Enter judge: ')
n1 = judge.count(1)
n2 = judge.count(2)
assert n1 != 0
sum1 = 0
sum2 = 0
L = len(judge)
for i in xrange(L):
if judge[i] == 1:
sum1 += score[i]
elif judge[i] == 2:
sum2 += score[i]
if n2 == 0:
aver = sum1/n1
else:
aver = (sum1//n1*0.6 + sum2//n2*0.4)//1
print aver
cal_score()
f = raw_input('Enter to end:')


最近在看Python..正好吃饭,随便写的,考虑不全面。
nij6173 2012-09-12
  • 打赏
  • 举报
回复
#cal_score.py
def cal_score(score = [],judge = []):
score = input('Enter score: ')
judge = input('Enter judge: ')
n1 = judge.count(1)
n2 = judge.count(2)
assert n1 != 0
sum1 = 0
sum2 = 0
L = len(judge)
for i in xrange(L):
if judge[i] == 1:
sum1 += score[i]
elif judge[i] == 2:
sum2 += score[i]
if n2 == 0:
aver = sum1/n1
else:
aver = (sum1//n1*0.6 + sum2//n2*0.4)//1
print aver
cal_score()
f = raw_input('Enter to end:')


最近在看Python..正好吃饭,随便写的,考虑不全面。
nij6173 2012-09-12
  • 打赏
  • 举报
回复

#cal_score.py
def cal_score(score = [],judge = []):
score = input('Enter score: ')
judge = input('Enter judge: ')
n1 = judge.count(1)
n2 = judge.count(2)
assert n1 != 0
sum1 = 0
sum2 = 0
L = len(judge)
for i in xrange(L):
if judge[i] == 1:
sum1 += score[i]
elif judge[i] == 2:
sum2 += score[i]
if n2 == 0:
aver = sum1/n1
else:
aver = (sum1//n1*0.6 + sum2//n2*0.4)//1
print aver
cal_score()
f = raw_input('Enter to end:')


最近在看Python...刚好吃饭,随便写写的,考虑不全面。
coderchenjingui 2012-09-12
  • 打赏
  • 举报
回复

int cal_score(int score[], int judge_type[], int n)
{
int specialNum=0;
int normalNum=0;
int specialScore=0;
int normalScore=0;
for(int i=0;i<n;i++)
{
if(judge_type[i]==1)
{
specialScore+=score[i];
specialNum++;
}
else if(judge_type[i]==2)
{
normalScore+=score[i];
normalNum++;
}
}
if(specialNum!=0&&normalNum!=0)
return int(specialScore/specialNum*0.6+normalScore/normalNum*0.4);
else if(specialNum!=0&&normalNum==0)
return (int)specialScore;

return -1;
}
xiaoji7561 2012-09-12
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 的回复:]
若是此题,我想我笔试能过,哈哈
另 这里 default:;//----舍弃不符要求数据 不要 break????
[/Quote]我也是,现在都还是菜鸟级的人
xiang_zi725 2012-09-12
  • 打赏
  • 举报
回复
#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++;
sum1 += score[i];
}
else
{
n2++;
sum2 += score[i];
}
}

if(0==n2)
{
return (sum1/n1);
}

return (sum1*0.6/n1 + sum2*0.4/n2);
}

void main()
{
int score[10]={1,2,3,5,6,7,8,6,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;
}
panda6661 2012-09-12
  • 打赏
  • 举报
回复
若是此题,我想我笔试能过,哈哈
另 这里 default:;//----舍弃不符要求数据 不要 break????
加载更多回复(17)

69,322

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧