2012届华为上机考试题目

9 篇文章 1 订阅

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) 

 

[cpp]  view plain  copy
 print ?
  1. #include<stdio.h>     
  2. #include<string.h>     
  3. #include<iostream.h>     
  4. #include<conio.h>     
  5. #define N 5     
  6.     
  7. int cal_score(int score[], int judge_type[], int n)     
  8.     
  9. {    
  10.     int expert=0;    
  11.     int dazhong=0;    
  12.     int zongfen=0;    
  13.     int i;    
  14.     int number=0;    
  15.         
  16.     for(i=0;i<N;i++)    
  17.     {    
  18.         if(judge_type[i]==1)    
  19.         {    
  20.             expert=expert+score[i];    
  21.             number++;    
  22.         }    
  23.         else dazhong=dazhong+score[i];    
  24.     }    
  25.     if(number==N)    
  26.     {    
  27.         zongfen=(int)(expert/N);    
  28.     }    
  29.     else    
  30.             
  31.     {    
  32.         expert=(int)(expert/number);    
  33.         dazhong=(int)(dazhong/(N-number));    
  34.         zongfen=int(0.6*expert+0.4*dazhong);    
  35.             
  36.     }    
  37.     return zongfen;    
  38.         
  39. }    
  40. int main()    
  41. {    
  42.     int score[N];    
  43.     int judge_type[N];    
  44.     int numberlast=0;    
  45.     int i;    
  46.     printf("please input the %d score:\n",N);    
  47.     for(i=0;i<N;i++)    
  48.         scanf("%d",&score[i]);    
  49.     printf("please input the level(1:expert,2:dazhong)\n");    
  50.     for(i=0;i<N;i++)    
  51.         scanf("%d",&judge_type[i]);    
  52.     numberlast=cal_score(score,judge_type,N);    
  53.     printf("the last score is %d\n",numberlast);    
  54.     return 0;    
  55. }    


 

运行结果分析:

please input the 5 score:
90 80 87 89 91
please input the level(1:expert,2:dazhong)
1 2 1 1 1
the last score is 85 

 

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}

[cpp]  view plain  copy
 print ?
  1. #include<stdio.h>     
  2. #include<string.h>     
  3. #include<conio.h>     
  4.     
  5.     
  6.     
  7. void sort(int input[], int n, int output[])    
  8. {    
  9.     int i,j;    
  10.     int k=1;    
  11.     int temp;    
  12.     int med;    
  13.     for(i=0;i<n;i++)    
  14.         for(j=0;j<n-i;j++)    
  15.             if(input[j]>input[j+1])    
  16.             {temp=input[j];input[j]=input[j+1];input[j+1]=temp;}    
  17.             if(n%2!=0)    
  18.             {    
  19.                 for(i=0;i<n;i++)    
  20.                     printf("%2d",input[i]);    
  21.                 printf("\n");    
  22.                 med=(n-1)/2;    
  23.                 output[med]=input[n-1];    
  24.                 for(i=1;i<=med;i++)    
  25.                 {    
  26.                     output[med-i]=input[n-1-k];    
  27.                     output[med+i]=input[n-2-k];    
  28.                     k=k+2;    
  29.                         
  30.                 }    
  31.             }    
  32.             else    
  33.             {    
  34.                     
  35.                 for(i=0;i<n;i++)    
  36.                     printf("%2d",input[i]);    
  37.                 printf("\n");    
  38.                 med=n/2;    
  39.                 output[med]=input[n-1];    
  40.                 for(i=1;i<=med-1;i++)    
  41.                 {    
  42.                     output[med-i]=input[n-1-k];    
  43.                     output[med+i]=input[n-2-k];    
  44.                     k=k+2;      
  45.                 }    
  46.                 output[0]=input[0];                 
  47.             }       
  48.             for(i=0;i<n;i++)    
  49.                     printf("%2d",output[i]);    
  50.                 printf("\n");    
  51. }    
  52.     
  53.     
  54. int main()    
  55. {    
  56.     int a[6]={3,6,1,9,7,8};    
  57.     int b[6]={0};    
  58.     for(int i=0;i<6;i++)    
  59.         printf("%2d",a[i]);    
  60.     printf("\n");    
  61.     sort(a,6,b);    
  62.     return 0;    
  63. }    


运行结果

3 6 1 9 7 8
1 3 6 7 8 9
1 6 8 9 7 3

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

 

[cpp]  view plain  copy
 print ?
  1. #include<stdio.h>     
  2. #include<string.h>     
  3. #include<malloc.h>     
  4. #include<iostream.h>     
  5.     
  6. void scheduler1(int task[], int n, int system_task[], int user_task[])    
  7. {    
  8.     int i;    
  9.     int j=0;    
  10.     int *p,*pp,*p_user,*pp_user;    
  11.     int index=0;    
  12.     int count,count2;    
  13.     int min=0;    
  14.     int k=0;    
  15.     p=(int*)malloc(sizeof(int)*n);    
  16.     for(i=0;i<n;i++)    
  17.         p[i]=0;    
  18.     pp=(int*)malloc(sizeof(int)*n);    
  19.     for(i=0;i<n;i++)    
  20.         pp[i]=0;    
  21.     p_user=(int*)malloc(sizeof(int)*n);    
  22.     for(i=0;i<n;i++)    
  23.         p_user[i]=0;    
  24.     pp_user=(int*)malloc(sizeof(int)*n);    
  25.     for(i=0;i<n;i++)    
  26.         pp_user[i]=0;    
  27.         
  28.     for(i=0;i<n;i++)    
  29.     {    
  30.         if(task[i]<50)    
  31.         {    
  32.             {    
  33.                 system_task[j]=task[i];    
  34.                 pp[j]=i;    
  35.                 j++;    
  36.             }    
  37.             count=j;    
  38.         }    
  39.             
  40.         else if(task[i]<=255)    
  41.         {    
  42.             
  43.             {    
  44.                 user_task[k]=task[i];    
  45.                 pp_user[k]=i;    
  46.                 k++;    
  47.             }    
  48.             count2=k;    
  49.         }    
  50.         else task[i]=task[i];    
  51.         
  52.     }    
  53.         
  54.     for(i=0;i<count;i++)    
  55.         printf("%3d",system_task[i]);    
  56.     printf("\n");    
  57.         
  58.         
  59.     for(i=0;i<count;i++)    
  60.     {    
  61.         min=system_task[0];    
  62.         for(j=1;j<count;j++)    
  63.         {    
  64.                 
  65.             if(system_task[j]<min)    
  66.             {    
  67.                 min=system_task[j];     
  68.                 p[i]=j;         
  69.             }    
  70.                 
  71.         }    
  72.         system_task[p[i]]=51;    
  73.     }    
  74.         
  75.     pp[count]=-1;    
  76.     for(i=0;i<count;i++)    
  77.         printf("%3d",pp[p[i]]);     
  78.     printf("%3d\n",pp[count]);    
  79.         
  80.         
  81.     /***********************************************************/    
  82.         
  83.     for(i=0;i<count2;i++)    
  84.         printf("%4d",user_task[i]);    
  85.     printf("\n");    
  86.         
  87.     for(i=0;i<count2;i++)    
  88.     {    
  89.         min=user_task[0];    
  90.         for(j=1;j<count2;j++)    
  91.         {    
  92.                 
  93.             if(user_task[j]<min)    
  94.             {    
  95.                 min=user_task[j];       
  96.                 p_user[i]=j;            
  97.             }    
  98.                 
  99.         }    
  100.         user_task[p_user[i]]=256;    
  101.     }    
  102.         
  103.     pp_user[count2]=-1;    
  104.     for(i=0;i<count2;i++)    
  105.         printf("%4d",pp_user[p_user[i]]);       
  106.     printf("%3d\n",pp_user[count2]);    
  107.         
  108.         
  109. }    
  110.     
  111.     
  112.     
  113. int main()    
  114. {    
  115.     int task[9]={0, 30, 155, 1, 80, 300,170, 40, 99};    
  116.     int system_task[9]={0};    
  117.     int user_task[9]={0};    
  118.     scheduler1(task,9,system_task,user_task);    
  119.     return 0;    
  120. }    


 

运行结果:

 0 30  1 40
 0  3  1  7 -1
155  80 170  99
  4   8   2   6 -1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值