`
java-mans
  • 浏览: 11389913 次
文章分类
社区版块
存档分类
最新评论

华为机试题(2)

 
阅读更多

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<stdio.h>
  2. #include<string.h>
  3. intcal_score(intscore[],intjudge_type[],intn)
  4. {
  5. inti;
  6. /*intlen_j=strlen(judge_type);*/
  7. intsum_exp=0,sum_nml=0;
  8. intcnt_exp=0,cnt_nml=0;
  9. intave_exp,ave_nml,aver_score;
  10. for(i=0;i<n;i++)
  11. {
  12. if(judge_type[i]==1)
  13. {
  14. sum_exp+=score[i];
  15. cnt_exp++;
  16. }
  17. else
  18. {
  19. sum_nml+=score[i];
  20. cnt_nml++;
  21. }
  22. }
  23. ave_exp=sum_exp/cnt_exp;
  24. if(cnt_nml==0)
  25. returnave_exp;
  26. ave_nml=sum_nml/cnt_nml;
  27. aver_score=ave_exp*0.6+ave_nml*0.4;
  28. returnaver_score;
  29. }
  30. intmain(/*intargc,char**argv*/)
  31. {
  32. intscore[5]={88,87,95,90,84};
  33. intjudge_type[5]={1,1,2,1,2};
  34. intav_score=cal_score(score,judge_type,5);
  35. printf("avscoreis%d\n",av_score);
  36. system("pause");
  37. return0;
  38. }


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<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. voidlarge2small(intnum[],intn)
  5. {
  6. inti,j,tmp;
  7. for(i=0;i<n-1;i++)
  8. {
  9. for(j=i+1;j<n;j++)
  10. if(num[i]<num[j])
  11. {
  12. tmp=num[i];
  13. num[i]=num[j];
  14. num[j]=tmp;
  15. }
  16. }
  17. }
  18. voidsort(intinput[],intn,intoutput[])
  19. {
  20. inti;
  21. intj;
  22. large2small(input,n);
  23. if(n%2==1)
  24. {
  25. output[(n-1)/2]=input[0];
  26. j=1;
  27. for(i=(n-1)/2-1;i>=0;i--)
  28. {
  29. output[i]=input[j];
  30. j+=2;
  31. }
  32. j=2;
  33. for(i=(n-1)/2+1;i<n;i++)
  34. {
  35. output[i]=input[j];
  36. j+=2;
  37. }
  38. }
  39. elseif(n%2==0)
  40. {
  41. output[(n)/2]=input[0];
  42. j=1;
  43. for(i=(n)/2-1;i>=0;i--)
  44. {
  45. output[i]=input[j];
  46. j+=2;
  47. }
  48. j=2;
  49. for(i=(n)/2+1;i<n;i++)
  50. {
  51. output[i]=input[j];
  52. j+=2;
  53. }
  54. }
  55. }
  56. intmain(/*intargc,char**argv*/)
  57. {
  58. intinput[6]={3,6,1,9,7,8};
  59. intoutput[6]={0};
  60. inti;
  61. sort(input,6,output);
  62. for(i=0;i<6;i++)
  63. printf("%d",output[i]);
  64. system("pause");
  65. return0;
  66. }

3、操作系统任务调度问题。操作系统任务分为系统任务和用户任务两种。其中,系统任务的优先级 < 50,用户任务的优先级 >= 50 <= 255。优先级大于255的为非法任务,应予以剔除。现有一任务队列task[],长度为ntask中的元素值表示任务的优先级,数值越小,优先级越高。函数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<stdio.h>
  2. #include<string.h>
  3. voidscheduler(inttask[],intn,intsystem_task[],intuser_task[])
  4. {
  5. inti,j,k,tmp;
  6. intr=0,s=0;
  7. int*task_order=(int*)malloc(n*sizeof(int));
  8. for(i=0;i<n;i++)
  9. task_order[i]=task[i];
  10. for(j=0;j<n-1;j++)
  11. for(k=j+1;k<n;k++)
  12. if(task_order[j]>task_order[k])
  13. {
  14. tmp=task_order[j];
  15. task_order[j]=task_order[k];
  16. task_order[k]=tmp;
  17. }
  18. for(i=0;i<n;i++)
  19. {
  20. if(task_order[i]<50)
  21. {
  22. for(j=0;j<n;j++)
  23. if(task_order[i]==task[j])
  24. system_task[r++]=j;
  25. }
  26. if(task_order[i]>=50&&task_order[i]<255)
  27. {
  28. for(j=0;j<n;j++)
  29. if(task_order[i]==task[j])
  30. user_task[s++]=j;
  31. }
  32. }
  33. system_task[r]=-1;
  34. user_task[s]=-1;
  35. free(task_order);
  36. }
  37. intmain(/*intargc,char**argv*/)
  38. {
  39. inttask[]={0,30,155,1,80,300,170,40,99};
  40. intsystem_task[9]={0};
  41. intuser_task[9]={0};
  42. inti;
  43. scheduler(task,9,system_task,user_task);
  44. for(i=0;i<9;i++)
  45. printf("%d",system_task[i]);
  46. printf("\n");
  47. for(i=0;i<9;i++)
  48. printf("%d",user_task[i]);
  49. system("pause");
  50. return0;
  51. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics