zoukankan      html  css  js  c++  java
  • 华为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)

    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[])

    3、操作系统任务调度问题。操作系统任务分为系统任务和用户任务两种。其中,系统任务的优先级 < 50,用户任务的优先级 >= 50且 <= 255。优先级大于255的为非法任务,应予以剔除。现有一任务队列task[],长度为n,task中的元素值表示任务的优先级,数值越小,优先级越高。函数scheduler实现如下功能,将task[] 中的任务按照系统任务、用户任务依次存放到 system_task[] 数组和 user_task[] 数组中(数组中元素的值是任务在task[] 数组中的下标),并且优先级高的任务排在前面,数组元素为-1表示结束。

    例如:task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99} system_task[] = {0, 3, 1, 7, -1} user_task[] = {4, 8, 2, 6, -1}

    函数接口 void scheduler(int task[], int n, int system_task[], int user_task[])

    参考答案(欢迎讨论)转载请注明来源http://www.cnblogs.com/jerry19880126/

    三道题写在一个CPP文件里了,如下:

      1 #include <iostream>
      2 using namespace std;
      3  
      4  
      5  
      6  //编程题第一题
      7  int cal_score(int score[], int judge_type[], int n)
      8  {
      9      const int EXPERT_TYPE = 1;
     10      //const int AUDIANCE_TYPE = 2;
     11      const double EXPERT_WEIGHT = 0.6;
     12      const double AUDIANCE_WEIGHT = 0.4;
     13      int expertSum = 0;
     14      int expertCount = 0;
     15      int audianceSum = 0;
     16      int audianceCount = 0;
     17      for(int i = 0; i < n; ++i)
     18      {
     19          if(judge_type[i] == EXPERT_TYPE)
     20          {
     21              expertSum += score[i];
     22              ++ expertCount;
     23          }
     24          else
     25          {
     26              audianceSum += score[i];
     27              ++ audianceCount;
     28          }    
     29      }
     30      //综合评分
     31      if(!audianceCount)
     32      {
     33          //只有专家评委
     34          return expertSum / n;
     35      }
     36      else
     37      {
     38          //两种类型评委都有
     39          return (int)( EXPERT_WEIGHT * (expertSum / expertCount) 
     40              + AUDIANCE_WEIGHT * (audianceSum / audianceCount) );
     41      }
     42  }
     43  
     44  
     45  //编程题第二题
     46  void sort(int input[], int n, int output[])
     47  {
     48      //先对input中的数进行降序排序
     49      //用插入法
     50      for(int i = 1; i < n; ++i)
     51      {
     52          int j = i - 1;
     53          int temp = input[i];
     54          while(j >=0 && input[j] < temp)
     55          {
     56              input[j+1] = input[j];
     57              --j;
     58          }
     59          input[j+1] = temp;
     60      }
     61      int center = n / 2;
     62      output[center] = input[0];
     63      int diff = 1;
     64      int i = 1;
     65      while(i < n)
     66      {
     67          output[center - diff] = input[i++];
     68          if(i >= n)
     69          {
     70              break;
     71          }
     72          output[center + diff] = input[i++];
     73          ++diff;
     74      }
     75  }
     76  
     77  //编程题第三题
     78  void scheduler(int task[], int n, int system_task[], int user_task[])
     79  {
     80      //先将任务分类,剔除不合理的任务
     81      int systemIndex = 0;
     82      int userIndex = 0;
     83      for(int i = 0; i < n; ++i)
     84      {
     85          if(task[i] < 50)
     86          {
     87              //系统任务
     88              system_task[systemIndex++] = i;//只存放下标
     89          }
     90          else if(task[i] <= 255)
     91          {
     92              //用户任务
     93              user_task[userIndex++] = i;
     94          }
     95      }
     96      
     97      //对系统任务排序,按优先级数值升序排序
     98      //用插入法排序
     99      for(int i = 1; i < systemIndex; ++i)
    100      {
    101          int j = i - 1;
    102          int temp = system_task[i];
    103          while(j >=0 && task[system_task[j]] > task[temp])
    104          {
    105              system_task[j+1] = system_task[j];
    106              --j;
    107          }
    108          system_task[j+1] = temp;
    109      }
    110  
    111      //对用户任务排序,思想同上
    112      for(int i = 1; i < userIndex; ++i)
    113      {
    114          int j = i - 1;
    115          int temp = user_task[i];
    116          while(j >=0 && task[user_task[j]] > task[temp])
    117          {
    118              user_task[j+1] = user_task[j];
    119              --j;
    120          }
    121          user_task[j+1] = temp;
    122      }
    123  
    124      //末尾处理
    125      system_task[systemIndex] = -1;
    126      user_task[userIndex] = -1;
    127  }
    128  
    129  
    130  void print(const int a[], const int n)
    131  {
    132      for(int i = 0; i < n; ++i)
    133      {
    134          cout << a[i] << "  ";
    135      }
    136      cout << endl;
    137  }
    138  
    139  int main()
    140  {
    141      //第一题测试样例
    142      cout << "第一题测试结果:" << endl;
    143      int score[10] =     {5,2,3,5,2,2,5,2,3,2};
    144      int judgeType[10] = {1,1,2,2,1,1,2,2,1,2};
    145      cout << cal_score(score, judgeType, 10) << endl << endl;
    146  
    147      //第二题测试样例
    148      cout << "第二题测试结果:"<< endl;
    149      int input1[5] = {3, 6, 1, 9, 7};
    150      int output1[5];
    151      sort(input1, 5, output1);
    152      print(output1, 5);
    153  
    154      int input2[6] = {3, 6, 1, 9, 7, 8};
    155      int output2[6];
    156      sort(input2, 6, output2);
    157      print(output2, 6);
    158      cout << endl;
    159  
    160      //第三题测试样例
    161      int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99};
    162      int system_task[9];
    163      int user_task[9];
    164      scheduler(task, 9, system_task, user_task);
    165      cout << "系统任务为:" << endl;
    166      print(system_task, 5);
    167      cout << "用户任务为:" << endl;
    168      print(user_task, 5);
    169  }
  • 相关阅读:
    Linux 下 MQ 的安装
    云计算的三种服务模式:IaaS,PaaS和SaaS
    Mac下安装Maven
    JDK Mac 安装
    Mac OS 终端利器 iTerm2
    单元测试用例规范
    boolean 属性的定义规范
    2019-12-9号 终于入职 阿里巴巴
    远程调试方法
    系统提测及上线规范(系统上线必读!)
  • 原文地址:https://www.cnblogs.com/jerry19880126/p/2623993.html
Copyright © 2011-2022 走看看