各大计算机公司 笔试及面试 题目 - 华为(上机 1-3)

备注:转载于 http://blog.csdn.net/ychtt/article/details/6774184


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)

 

  1. #include "iostream"  
  2. using namespace std;  
  3.   
  4. int cal_score(int score[], int judge_type[], int n)   
  5. {  
  6.  if (NULL==score||NULL==judge_type||0==n)  
  7.   return 0;  
  8.   
  9.  int sum=0;  
  10.  int sum1=0,count1=0;  
  11.  int sum2=0,count2=0;  
  12.  for(int i=0;i<n;i++)  
  13.  {  
  14.   if (judge_type[i]==1)  
  15.   {  
  16.    sum1=sum1+score[i];  
  17.    count1++;  
  18.   }  
  19.   else  
  20.   {  
  21.    sum2=sum2+score[i];  
  22.       count2++;  
  23.   }  
  24.  }  
  25.  if(0==count2)  
  26.   sum=sum1/count1;  
  27.  else  
  28.   sum=(sum1/count1)*0.6+(sum2/count2)*0.4;  
  29.  return sum;  
  30. }  
  31. void main()  
  32. {  
  33.  int score[3]={12,13,15};  
  34.  int judge_type[3]={1,1,2};  
  35.  printf("%d",cal_score(score, judge_type, 3) );  
  36.   
  37. }  
  38.   
  39.    

 


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


 

  1. #include "iostream"  
  2. using namespace std;  
  3. void bubblesort(int data[],int n)  
  4. {  
  5.  int temp=0;  
  6.  for(int i=0;i<n;i++ )  
  7.  {  
  8.   for (int j=i+1;j<n;j++)  
  9.   {  
  10.    if (data[i]<data[j])  
  11.    {  
  12.     temp=data[i];  
  13.     data[i]=data[j];  
  14.     data[j]=temp;  
  15.    }  
  16.   }  
  17.  }  
  18. }  
  19.   
  20. void sort(int input[], int n, int output[])  
  21. {  
  22.  int *sort_input=new int[n];  
  23.  for(int i=0;i<n;i++)  
  24.  {  
  25.   sort_input[i]=input[i];  
  26.  }  
  27.  bubblesort(sort_input,n);  
  28.  if (1==n%2)  
  29.  {  
  30.   int mid=n/2;  
  31.   int k=0;  
  32.   output[mid]=sort_input[k++];  
  33.   for(int j=1;j<=n/2;j++)  
  34.   {  
  35.    output[mid-j]=sort_input[k++];  
  36.    output[mid+j]=sort_input[k++];  
  37.   }  
  38.     
  39.  }   
  40.  else  
  41.  {  
  42.   int mid=n/2;  
  43.   int k=0;  
  44.   output[mid]=sort_input[k++];  
  45.   for(int j=1;j<n/2;j++)  
  46.   {  
  47.    output[mid-j]=sort_input[k++];  
  48.    output[mid+j]=sort_input[k++];  
  49.   }  
  50.   output[0]=sort_input[k++];  
  51.     
  52.  }  
  53.    
  54.     delete sort_input;  
  55. }  
  56.   
  57.   
  58. void main()  
  59. {  
  60.  int input1[] = {3, 6, 1, 9, 7};  
  61.  int output1[5];  
  62.  memset(output1,0,5*sizeof(int));  
  63.  int input2[] = {3, 6, 1, 9, 7, 8}  ;   
  64.  int output2[6];  
  65.  memset(output2,0,6*sizeof(int));  
  66.    
  67.  sort(input1, 5, output1);  
  68.  sort(input2, 6, output2);  
  69.  for(int k=0;k<5;k++)  
  70.   printf("%d   ",output1[k]);  
  71.  for(k=0;k<6;k++)  
  72.   printf("%d   ",output2[k]);  
  73. }  


 


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

 

  1. #include "iostream"  
  2. using namespace std;  
  3. void change(int *a,int *b)  
  4. {  
  5.  int temp=*a;  
  6.  *a=*b;  
  7.  *b=temp;  
  8. }  
  9.   
  10. void bubblesort(int data[],int n,int index[])//冒泡排序并记录排序后下标  
  11. {  
  12.  int temp=0;  
  13.  for(int j=0;j<n;j++)  
  14.   index[j]=j;  
  15.  for(int i=0;i<n;i++ )  
  16.  {  
  17.   for (int j=i+1;j<n;j++)  
  18.   {  
  19.    if (data[i]>data[j])  
  20.    {  
  21.     change(&data[i],&data[j]);  
  22.     change(&index[i],&index[j]);  
  23.    }  
  24.   }  
  25.     
  26.  }  
  27. }  
  28.   
  29. void scheduler(int task[], int n, int system_task[], int user_task[])  
  30. {  
  31.    
  32.  int *sort_task=new int[n];  
  33.  int *index=new int[n];  
  34.  for(int i=0;i<n;i++)  
  35.  {  
  36.   sort_task[i]=task[i];  
  37.  }  
  38.  bubblesort(sort_task,n,index);  
  39.  i=0;  
  40.  while (sort_task[i]<50)  
  41.  {  
  42.   system_task[i]=index[i];  
  43.   i++;  
  44.  }  
  45.    
  46.     system_task[i]=-1;  
  47.    
  48.  int k=0;  
  49.  while (sort_task[i]>50&&sort_task[i]<=255)  
  50.  {  
  51.   user_task[k++]=index[i++];  
  52.  }  
  53.    
  54.     user_task[k]=-1;  
  55.  delete sort_task;  
  56.  delete index;  
  57.    
  58.    
  59. }  
  60. void main()  
  61. {  
  62.  int task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99} ;  
  63.  int n=sizeof(task)/sizeof(int);  
  64.  int *system_task=new int[n];   
  65.  int *user_task=new int[n];   
  66.    
  67.     scheduler(task, n,  system_task, user_task);  
  68.    
  69.     for(int k=0;k<n;k++)  
  70.    {  
  71.       
  72.     printf("%d   ",system_task[k]);  
  73.     if (system_task[k]!=-1)  
  74.     {  
  75.      printf("%d   ",system_task[k]);  
  76.     }  
  77.     else  
  78.     {  
  79.      printf("%d   ",system_task[k]);  
  80.      break;}  
  81.       
  82.    }  
  83.    printf("\n");  
  84.    for(k=0;k<n;k++)  
  85.    {  
  86.     printf("%d   ",user_task[k]);  
  87.     if (user_task[k]!=-1)  
  88.     {  
  89.      printf("%d   ",user_task[k]);  
  90.     }  
  91.     else{  
  92.      printf("%d   ",system_task[k]);  
  93.      break;}  
  94.       
  95.    }  
  96. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值