一道华为上机题

题目描述: 选秀节目打分,分为专家评委和大众评委,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;
}这样可以吗?请求帮助



最佳答案:

          
          
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; }



 
 
#1楼 得分:0回复于:2012-09-07 16:24:06
你的sum与n都是整数,除得的结果是sum mod n,题目要求是平均分取整,所以你的有问题。
可以用double sum,平均分结果取整。
 
#2楼 得分:5回复于:2012-09-07 16:53:47
我没看你的程序,就看了题目,自己试着在纸上写了一下,然后和你的一对,就发觉差别了。先不说正确不正确,只谈效率问题。
那个功能函数中,其实只需要完整的执行一次for循环就能得出结果了,你用了3个for循环,而且代码可以精简很多,效率也能高很多
 
#3楼 得分:0回复于:2012-09-07 17:07:11
引用楼主 的回复:
for(int k=0;k<n;k++)
{
if(1==judge_type[k])
sum1+=judge_type[k];
else
sum2+=judge_type[k];
}

这几句你想表达的是什么,你仔细瞧瞧,和分数有什么关系么,应该是score[k]吧,即使换成score[k],结果也是错的
 
#4楼 得分:0回复于:2012-09-07 17:16:08
引用 1 楼 的回复:
你的sum与n都是整数,除得的结果是sum mod n,题目要求是平均分取整,所以你的有问题。
可以用double sum,平均分结果取整。

正确性和代码整洁程度都不太好。。
不过华为要求很低,基本不用担心
 
#5楼 得分:0回复于:2012-09-07 20:42:09
引用 2 楼 的回复:

我没看你的程序,就看了题目,自己试着在纸上写了一下,然后和你的一对,就发觉差别了。先不说正确不正确,只谈效率问题。
那个功能函数中,其实只需要完整的执行一次for循环就能得出结果了,你用了3个for循环,而且代码可以精简很多,效率也能高很多

谢谢你的批评指正,我写的的确很烂,恩,我重新写了一遍,希望你能指点一下,万分感激
#include<iostream>
using namespace std;
int cal_score(int score[], int judge_type[], int n)
{
int n1=0,n2=0;
double sum1=0,sum2=0;
int avarage;
for(int i=0;i<n;i++)
{
if(1==judge_type[i])
{
sum1+=score[i];
n1++;
}
else
{
sum2+=score[i];
n2++;
}
}
if(0==n2)
{
  avarage=(int)(sum1/n);
return avarage;
}
else
{
avarage=(int)((int)(sum1/n1*0.6)+(int)(sum2/n2*0.4));
return avarage;
}
}
 
#6楼 得分:10回复于:2012-09-07 20:59:49
int强制转换是舍弃小数,而题中的取整的意思,我觉得应该是按照四舍五入。
 
#8楼 得分:0回复于:2012-09-08 00:27:58
#include<stdio.h>

int cal_score(int score[], int judge_type[], int n);


void main()
{
int n=5, score[5]={80,90,70,60,50}, judge_type[5]={1,2,1,2,1};

printf("%d\n",cal_score(score,judge_type,n) );
}


int cal_score(int score[], int judge_type[], int n)
{
int i,sum1=0,sum2=0,a=0,b=0;
for(i=0;i<n;i++)

if(judge_type[i]==1) { sum1+=score[i]; a=a+1; }

  else { sum2+=score[i]; b=b+1; }

return (sum1/a*0.6+sum2/b*0.4);
}

//夜深了我也写了一个,你自己慢慢看看吧! 他限制了函数入口不合适用double等类型.不过还是符合要求的.希望对你有帮助...
 
#9楼 得分:0回复于:2012-09-08 00:29:29
#include<stdio.h>

int cal_score(int score[], int judge_type[], int n);


void main()
{
int n=5, score[5]={80,90,70,60,50}, judge_type[5]={1,2,1,2,1};

printf("%d\n",cal_score(score,judge_type,n) );
}


int cal_score(int score[], int judge_type[], int n)
{
int i,sum1=0,sum2=0,a=0,b=0;
for(i=0;i<n;i++)

if(judge_type[i]==1) { sum1+=score[i]; a=a+1; }

  else { sum2+=score[i]; b=b+1; }

return (sum1/a*0.6+sum2/b*0.4);
}

 
#10楼 得分:0回复于:2012-09-08 08:31:00
我有一个问题:是不是专业评委个数为0的情况也应考虑一下,否则在计算“专家评委和大众评委的分数先分别取一个平均分(平均分取整)”会有除零的情况
 
#11楼 得分:0回复于:2012-09-08 09:05:13
那你在加一个判断是否大众评委的if判断语句.使用不同的两个返回值.这个应该很简单的..
 
#12楼 得分:0回复于:2012-09-08 10:10:07
刚才看了看考试要求“评卷人保证测试用例输入参数的合法性,考生不用考虑输入参数非法或异常
的情况”所以这种情况应不用考虑
 
#13楼 得分:20回复于:2012-09-08 10:32:50
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; }
不想写default及注释语句,可是给新人看不得不写啊
 
#14楼 得分:0回复于:2012-09-08 11:05:42
引用 13 楼 的回复:
if(n&amp;&amp;Score&amp;&amp;Judge_type)
多谢,程序简洁明了。只是n初始化为0所以函数体不会被执行,此处应该是N?
 
#15楼 得分:5回复于:2012-09-08 11:07:40
先写为double,再强制转换为int吧,否则结果可能不正确
 
#16楼 得分:0回复于:2012-09-08 11:44:17
对是N不是n,笔误了
 
#17楼 得分:0回复于:2012-09-09 10:02:06
引用 13 楼 的回复:
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&amp;&amp;Score&amp;&amp;Judge_type){
for(int i=0;i<N;++i)
switch(Judge_type……


牛人啊
 
#18楼 得分:0回复于:2012-09-12 11:53:23
C/C++ code
            
            
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 ; }
 
#19楼 得分:0回复于:2012-09-12 12:55:05
[code=Python][/code]
#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...刚好吃饭,随便写写的,考虑不全面。
 
#20楼 得分:0回复于:2012-09-12 12:58:48
[code=Python][/code]#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..正好吃饭,随便写的,考虑不全面。
 
#21楼 得分:0回复于:2012-09-12 12:59:06
[code=Python][/code]#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..正好吃饭,随便写的,考虑不全面。
 
#23楼 得分:0回复于:2012-09-12 13:29:45
我靠。。Try again:
Python code
            
            
# 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: ' )
 
#24楼 得分:0回复于:2012-09-12 13:39:40
若是此题,我想我笔试能过,哈哈 
另 这里 default:;//----舍弃不符要求数据 不要 break????
 
#25楼 得分:0回复于:2012-09-12 14:46:21
#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;
}
 
#26楼 得分:0回复于:2012-09-12 16:09:54
引用 24 楼 的回复:
若是此题,我想我笔试能过,哈哈
另 这里 default:;//----舍弃不符要求数据 不要 break????
我也是,现在都还是菜鸟级的人

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值