歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

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頁精彩內容: http://www.linuxidc.com/Linux/2013-09/90495p2.htm

Copyright © Linux教程網 All Rights Reserved