
2012届华为校园招聘上机考试题目(9月6日下午1点场)
分类: 华为准备 2011-09-08 15:10 281人阅读 评论(0) 收藏 举报
在网上看到华为在有的地方已经开始机试了,于是决定自己先编着试试。下面是题目和自己写的代码。
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)
view plaincopy to clipboardprint?
1.#include 2.#include 3.#include 4.#include 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 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 49. printf("please input the level(1:expert,2:dazhong)\\n"); 50. for(i=0;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 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} view plaincopy to clipboardprint? 1.#include 2.#include 3.#include 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 16. {temp=input[j];input[j]=input[j+1];input[j+1]=temp;} 17. if(n%2!=0) 18. { 19. for(i=0;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 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 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[]) view plaincopy to clipboardprint? 1.#include 2.#include 3.#include 4.#include 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 18. pp=(int*)malloc(sizeof(int)*n); 19. for(i=0;i 21. p_user=(int*)malloc(sizeof(int)*n); 22. for(i=0;i 24. pp_user=(int*)malloc(sizeof(int)*n); 25. for(i=0;i 27. 28. for(i=0;i 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 56. printf("\\n"); 57. 58. 59. for(i=0;i 61. min=system_task[0]; 62. for(j=1;j . 65. if(system_task[j] 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 78. printf("%3d\\n",pp[count]); 79. 80. 81. /***********************************************************/ 82. 83. for(i=0;i 85. printf("\\n"); 86. 87. for(i=0;i . min=user_task[0]; 90. for(j=1;j 92. 93. if(user_task[j] 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 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 2012华为校园招聘机试(成都)-1 分类: 华为准备 2011-09-08 20:09 267人阅读 评论(0) 收藏 举报 第一道就是说算分数的问题,去掉一个最高分一个最低分,求平均分 view plaincopy to clipboardprint? 1.#include 2.float avescore(float score[],int n) 3.{ 4. float min=0; 5. float max=0; 6. int minindex=0; 7. int maxindex=0; 8. float sum=0; 9. min=score[0]; 10. for(int i=0;i 13. min=score[i]; 14. minindex=i; 15. } 16. score[minindex]=0; 17. max=score[0]; 18. for(i=0;i 20. { 21. max=score[i]; 22. maxindex=i; 23. } 24. score[maxindex]=0; 25. for(i=0;i 27. sum=sum/(n-2); 28. return sum; 29.} 30.void main() 31.{ 32. float score[6]={70,80,90,98,87,86}; 33. float lastscore; 34. lastscore=avescore(score,6); 35. printf("the last score is :%5.2f\\n",lastscore); 36. 37.} 运行结果: the last score is :85.75 2012华为校园招聘机试(成都)-2 分类: 华为准备 2011-09-08 22:23 324人阅读 评论(2) 收藏 举报 第二道:对一个数组,将数组中偶数从大到小排序,奇数从小到大排序,奇数和偶数交叉着放且输出数组第一位放奇数 若奇数和偶数不等长,则把剩下的直接放到数组中。 -------------------思路:先进行奇偶判断,得到奇数和偶数数组。然后对两数组排序,进行长度判断,最后组织数据。 view plaincopy to clipboardprint? 1.#include 2.#include 3. 4.void jiou(int a[],int n) 5.{ 6. int *p1; 7. int *p2; 8. int i,j; 9. int k=0; 10. int kk=0; 11. int count1=0; 12. int count2=0; 13. int temp; 14. int temp2; 15. int m=0; 16. p1=(int*)malloc(sizeof(int)*n); 17. p2=(int*)malloc(sizeof(int)*n); 18. for(i=0;i 20. p1[i]=0; 21. p2[i]=0; 22. } 23. 24. for(i=0;i 26. 27. if((a[i]%2)!=0) 28. {p2[kk++]=a[i];} 29. else 30. {p1[k++]=a[i];} 31. } 32. count1=k; 33. count2=kk; 34. 35. for(i=0;i 37. printf("\\n"); 38. 39. for(i=0;i 42. {temp2=p2[j];p2[j]=p2[j+1];p2[j+1]=temp2;} 43. for(i=0;i 45. printf("\\n"); 46. 47. for(i=0;i 49. printf("\\n"); 50. 51. 52. for(i=0;i 56. for(i=0;i 58. printf("\\n"); 59. 60. 61. 62. if(count1>count2) 63. . { 65. for(i=0;i 67. a[i+m]=p2[i]; 68. a[i+1+m]=p1[i]; 69. m=m+1; 70. } 71. for(i=0;i 73. 74. } 75. else 76. { 77. for(i=0;i 79. a[i+m]=p2[i]; 80. a[i+1+m]=p1[i]; 81. m=m+1; 82. 83. } 84. for(i=0;i 86. 87. } 88. for(i=0;i 90. printf("%\\n"); 91. 92.} 93.void main() 94.{ 95. int a[10]={2,3,14,6,2,15,12,14,4,11}; 96. jiou(a,10); 97. 98.} 运行结果: 3 15 11 3 11 15 2 14 6 2 12 14 4 14 14 12 6 4 2 2 3 14 11 14 15 12 6 4 2 2
