2012届华为校园招聘机试题


1、选秀节目打分,分为专家评委和大众评委,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)

int cal_score(int score[], int judge_type[], int n)
{
 int i , TotalExpertScore , TotalPublictScore , ExpertNum , PublicNum;
 double res = 0;
 TotalExpertScore = TotalPublictScore = 0;
 ExpertNum = PublicNum = 0;
 for(i = 0 ; i < n ; ++i)
 {
  if(1 == judge_type[i])      //专家评委
  {
   TotalExpertScore += score[i];
   ++ExpertNum;
  }
  else                        //大众评委
  {
   TotalPublictScore += score[i];
   ++PublicNum;
  }
 }
 if(0 == PublicNum)              //没有大众评委
 {
  return TotalExpertScore/ExpertNum;
 }
 else
 {
  res = 0.6*TotalExpertScore/ExpertNum + 0.4*TotalPublictScore/PublicNum;
  return (int)res;
 }
}

2、给定一个数组input[] ,如果数组长度n为奇数,则将数组中最大的元素放到 output[] 数组最中间的位置,如果数组长度n为偶数,则将数组中最大的元素放到 output[] 数组中间两个位置偏右的那个位置上,然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。
例如:input[] = {3, 6, 1, 9, 7} output[] = {3, 7, 9, 6, 1}; input[] = {3, 6, 1, 9, 7, 8} output[] = {1, 6, 8, 9, 7, 3}
函数接口 void sort(int input[[, int n, int output[])

void sort(int input[] , int n , int output[])
{
 int i , j , k , temp , mid;
 for(i = 0 ; i < n - 1 ; ++i)      //冒泡排序(降序)
 {
  for(j = 0 ; j < n - i - 1 ; ++j)
  {
   if(input[j] < input[j+1])
   {
    temp = input[j];
    input[j] = input[j+1];
    input[j+1] = temp;
   }
  }
 }//for
 mid = n>>1;
 j = mid - 1;
 k = mid + 1;
 output[mid] = input[0];
 for(i = 1 ; i < n ; )              //按照一左一右的顺序
 {
  if(j >= 0)
   output[j--] = input[i++];
  if(k < n)
   output[k++] = input[i++];
 }
}

接下来请看第2页精彩内容

  • 1
  • 2
  • 下一页

相关内容