华为上机题——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)
1.#include "iostream"
https://www.sodocs.net/doc/f09690621.html,ing 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;i13. {
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;
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.}
//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[])
print?
1.#include "iostream"
https://www.sodocs.net/doc/f09690621.html,ing namespace std;
3.void bubblesort(int data[],int n)
4.{
5.int temp=0;
6. for(int i=0;i7. {
8. for (int j=i+1;j9. {
10. if (data[i]11. {
12. temp=data[i];
13. data[i]=data[j];
14. data[j]=temp;
15. }
16. }
17. }
18.}
20.void sort(int input[], int n, int output[])
21.{
22.int *sort_input=new int[n];
23. for(int i=0;i24. {
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;j46. {
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"
https://www.sodocs.net/doc/f09690621.html,ing 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;j14. index[j]=j;
15. for(int i=0;i16. {
17. for (int j=i+1;j18. {
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;i35. {
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;k70. {
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;k85. {
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.}
4.删除字符串中所有给定的子串
问题描述:
在给定字符串中查找所有特定子串并删除,如果没有找到相应子串,则不作任何操作。要求实现函数:
int delete_sub_str(const char *str, const char *sub_str, char *result_str)
【输入】str:输入的被操作字符串
sub_str:需要查找并删除的特定子字符串
【输出】result_str:在str字符串中删除所有sub_str子字符串后的结果
【返回】删除的子字符串的个数
[cpp]view plaincopyprint?
1.#include
2.#include
3.#include "string"
4.int delete_sub_str(const char *str, const char *sub_str, char *result_str
)
5.{
6.int count = 0;
7.int k=0;
8.int n=strlen(str);
9. for (int i=0;i10. {
11. while (str[i]!=sub_str[0]&&str[i]!='\0')
12. {
13. result_str[k++]=str[i];
14. i++;
15. }
16.int j=0;
17.int temp=i;
18. while (str[i]==sub_str[j]&&str[i]!='\0'&&sub_str[j]!='\0')
19. {
20. i++;
21. j++;
22. }
23. if (sub_str[j]=='\0')
24. {
25. count++;
26. }
27. else
28. {
29. while (temp
30. {
31. result_str[k++]=str[temp];
32. temp++;
33. }
34.
35.
36. }
37.
38. i--;
39. }
40. result_str[k]='\0';
41. return count;
42.
43.}
44.
45.int main()
46.{
47.char *str = "aadde";
48.char *sub = "ad";
49.char res[50] ="";
50.int count = delete_sub_str(str, sub, res);
51. printf("子字符串的个数是:%d\n", count);
52. printf("删除子字符串后:%s\n", res);
53. return 0;
54.}
55.
56.
6.数组比较
问题描述:
比较两个数组,要求从数组最后一个元素开始逐个元素向前比较,如果2个数组长度不等,则只比较较短长度数组个数元素。请编程实现上述比较,并返回比较中发现的不相等
元素的个数
比如:
数组{1,3,5}和数组{77,21,1,3,5}按题述要求比较,不相等元素个数为0
数组{1,3,5}和数组{77,21,1,3,5,7}按题述要求比较,不相等元素个数为3
要求实现函数:
int array_compare(int len1, int array1[], int len2, int array2[])
【输入】int len1:输入被比较数组1的元素个数;
int array1[]:输入被比较数组1;
int len2:输入被比较数组2的元素个数;
int array2[]:输入被比较数组2;
【输出】无
【返回】不相等元素的个数,类型为int
示例
1)输入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5},int len2 = 5 函数返回:0
2)输入:int array1[] = {1,3,5},int len1 = 3,int array2[] = {77,21,1,3,5,7},int len2 = 6 函数返回:3
[cpp]view plaincopyprint?
1.#include
2.#include
3.#include "string"
4.#include "assert.h"
5.int array_compare(int len1, int array1[], int len2, int array2[])
6.{
7. assert(array1!=NULL&&array1!=NULL);
8.int count=0;
9. while (len1!=0&&len2!=0)
10. {
11. if (array1[len1-1]!=array2[len2-1])
12. {
13. count++;
14. }
15. len1--;
16. len2--;
17. }
18. return count;
19.
20.}
21.
22.int main()
23.{
24.int array1[] = {1,3,5};
25.int len1 = 3;
26.int array2[] = {77,21,1,3,5};
27.int len2 = 5;
28. printf("%d",array_compare(len1, array1,len2, array2));
29.int array3[] = {1,3,5};
30. len1 = 3;
31.int array4[] = {77,21,1,3,5,7};
32. len2 = 6;
33. printf("%d",array_compare(len1, array3,len2, array4));
34.}
7.约瑟夫问题
问题描述:
输入一个由随机数组成的数列(数列中每个数均是大于0的整数,长度已知),和初始计数值m。从数列首位置开始计数,计数到m后,将数列该位置数值替换计数值m,并将数列
该位置数值出列,然后从下一位置从新开始计数,直到数列所有数值出列为止。如果计数到达数列尾段,则返回数列首位置继续计数。请编程实现上述计数过程,同时输出数
值出列的顺序
比如:输入的随机数列为:3,1,2,4,初始计数值m=7,从数列首位置开始计数(数值3所在位置)
第一轮计数出列数字为2,计数值更新m=2,出列后数列为3,1,4,从数值4所在位置从新开始计数
第二轮计数出列数字为3,计数值更新m=3,出列后数列为1,4,从数值1所在位置开始计数
第三轮计数出列数字为1,计数值更新m=1,出列后数列为4,从数值4所在位置开始计数
最后一轮计数出列数字为4,计数过程完成。
输出数值出列顺序为:2,3,1,4。
要求实现函数:
void array_iterate(int len, int input_array[], int m, int output_array[])
【输入】int len:输入数列的长度;
int intput_array[]:输入的初始数列
int m:初始计数值
【输出】int output_array[]:输出的数值出列顺序
【返回】无
示例
输入:int input_array[] = {3,1,2,4},int len = 4,m=7
输出:output_array[] = {2,3,1,4}
[cpp]view plaincopyprint?
1.#include "iostream"
https://www.sodocs.net/doc/f09690621.html,ing namespace std;
3.struct Node
4.{
5.int data;
6. Node *next;
7.};
8.void array_iterate(int len, int input_array[], int m, int output_array[])
9.{
10. if (NULL==input_array||0==m||0==len)
11. return 0;
12. Node *node=new Node;
13. Node *head=new Node;
14. head->data=input_array[0];
15. Node *p=head;
16.int k=0;
17. for (int i=1;i18. {
19. node=new Node;
20. node->data=input_array[i];
21. p->next=node;
22. p=node;
23. }
24. p->next=head;
25. p=head;
26. Node *q=new Node;
27.int count;
28. while (p->next!=p)
29. {
30. count=1;
31. while (count32. {
33. q=p;
34. p=p->next;
35. count++;
36. }
37. m=p->data;
38. output_array[k++]=m;
39. q->next=p->next;
40. p=q->next;
41. }
42. output_array[k]=p->data;
43.}
44.void main()
45.{
46.int a[]={3,1,2,4};
47.int b[4];
48. memset(b,0,4*sizeof(int));
49. array_iterate(4, a, 7,b);
50. printf("%d,%d,%d,%d",b[0],b[1],b[2],b[3]);
51.}
8. 简单四则运算
问题描述:
输入一个只包含个位数字的简单四则运算表达式字符串,计算该表达式的值
注:1、表达式只含+, -, *, / 四则运算符,不含括号
2、表达式数值只包含个位整数(0-9),且不会出现0作为除数的情况
3、要考虑加减乘除按通常四则运算规定的计算优先级
4、除法用整数除法,即仅保留除法运算结果的整数部分。比如8/3=2。输入表达式保证无0作为除数情况发生
5、输入字符串一定是符合题意合法的表达式,其中只包括数字字符和四则运算符字符,除此之外不含其它任何字符,不会出现计算溢出情况
要求实现函数:
int calculate(int len,char *expStr)
【输入】int len: 字符串长度;
char *expStr: 表达式字符串;
【输出】无
【返回】计算结果
示例
1)输入:char *expStr = “1+4*5-8/3”
函数返回:19
2)输入:char *expStr = “8/3*3”
函数返回:6
[cpp]view plaincopyprint?
1.#include
2.#include
3.#include "string"
4.#include "assert.h"
5.struct stack{ //存放后续排列的表达式,模拟栈
6.char str[80];
7.int top;
8.};
9.struct sstack{ //存放计算表达式的值,模拟栈
10.int str[80];
11.int top;
12.};
13.int calculate(int len,char *expStr)
14.{
15.char *postexp=new char[len+1];
16. stack opstack;
17. sstack calstack;
18. calstack.top=-1;
19. opstack.top=-1;
20.int i=0;
21.int k=0;
22. while(expStr[i]!='\0')
23. {
24. if (expStr[i]>='0'&&expStr[i]<='9')
25. {
26. postexp[k++]=expStr[i];
27. }
28. else if (expStr[i]=='+'||expStr[i]=='-')
29. {
30. while (opstack.top>=0)
31. {
32. postexp[k++]=opstack.str[opstack.top--];
33. }
34. opstack.top++;
35. opstack.str[opstack.top]=expStr[i];
36.
37. }
38. else if (expStr[i]=='*'||expStr[i]=='/')
39. {
40. while (opstack.top>=0&&(opstack.str[opstack.top]=='*'||opstac
k.str[opstack.top]=='/'))
41. {
42. postexp[k++]=opstack.str[opstack.top--];
43. }
44. opstack.top++;
45. opstack.str[opstack.top]=expStr[i];
46. }
47.
48. i++;
49. }
50. while (opstack.top>=0)
51. {
52. postexp[k++]=opstack.str[opstack.top--];
53. }
54.int temp1=0;
55.int temp2=0;
56. for (i=0;i57. {
58. if (postexp[i]>='0'&&postexp[i]<='9')
59. {
60. calstack.top++;
61. calstack.str[calstack.top]=postexp[i]-'0';
62. }
63. else if (postexp[i]=='+')
64. {
65. temp1=calstack.str[calstack.top--];
66. temp2=calstack.str[calstack.top];
67. calstack.str[calstack.top]=temp2+temp1;
68. }
69. else if (postexp[i]=='-')
70. {
71. temp1=calstack.str[calstack.top--];
72. temp2=calstack.str[calstack.top];
73. calstack.str[calstack.top]=temp2-temp1;
74. }
75. else if (postexp[i]=='*')
76. {
77. temp1=calstack.str[calstack.top--];
78. temp2=calstack.str[calstack.top];
79. calstack.str[calstack.top]=temp2*temp1;
80. }
81. else if (postexp[i]=='/')
82. {
83. temp1=calstack.str[calstack.top--];
84. temp2=calstack.str[calstack.top];
85. calstack.str[calstack.top]=temp2/temp1;
86. }
87. }
88. printf("%d",calstack.str[calstack.top]);
89. return calstack.str[calstack.top];
90.
91.}
92.int main()
93.{
94.char *expStr = "1+4*5-8/3";
95.int len=strlen(expStr);
96. calculate(len,expStr);
97.
98.}
9.
一副牌中发五张扑克牌给你:让你判断数字的组成:
有以下几种情况:
1:四条:即四张一样数值的牌(牌均不论花色)2:三条带一对
3:三条带两张不相同数值的牌
4:两对
5:顺子包括10,J,Q,K,A
6:什么都不是
7:只有一对
[cpp]view plaincopyprint?
1.void sort(int data[],int n)
2.{
3.int temp=0;
4. for(int i=0;i5. {
6. for (int j=i+1;j7. {
8. if (data[i]9. {
10. temp=data[i];
11. data[i]=data[j];
12. data[j]=temp;
13. }
14. }
15. }
16.}
17.
18.void test(int a[],int len)
19.{
20.int *b=new int[len];
21.int count=0;
22.bool temp=false;
23. for (int i=0;i24. {
25. b[i]=a[i];
26. }
27. sort(b,5);
28. for (i=0;i29. {
30. if (b[i]==b[i+1])
31. count++;
32. }
33. switch (count)
34. {
35. case 0:
36. if (b[0]-b[4]==4||(b[0]-b[3]==3&&b[4]==1))
37. {
38. printf("顺子");
39. }
40. else
41. printf("什么都不是");
42. break;
43. case 1:
44. printf("只有一对");
45. break;
46. case 2:
47. for (i=0;i<3;i++)
48. {
49. if (b[i]==b[i+2])
50. {
51. printf("三条带两张不相同数值的牌");
52. temp=true;
53. break;
54. }
55. }
56. if (!temp)
57. {
58. printf("两对");
59. }
60. break;
61. case 3:
62. if (b[1]==b[3])
63. printf("四条:即四张一样数值的牌");
64. else
65. printf("三条带一对");
66.
67. break;
68.
69. }
70.}
71.
72.int main()
73.{
74.int a[5]={1,10,11,12,13};
75. test(a,5);
76.}
华为校园招聘上机题
目录 样题-初级题:从考试成绩中划出及格线 (2) 样题-中级题:亮着电灯的盏数 (2) 样题-高级题:地铁换乘 (3) 8.29去掉最大值、最小值之后剩下的个数 (3) 8.29从5个人中选取2个人作为礼仪 (4) 8.29验证括号是否匹配 (5) 8.31回文数 (5) 8.31将第一行中含有第二行中“23”的数输出并排序 (6) 8.31翻译电话号码 (6) 9.1.AM 将整数倒序输出,剔除重复数据 (7) 9.1.AM 大数相减 (8) 9.1.AM 判断if语句括号是否合法 (8) 9.1.PM (8) 9.1.PM (9) 9.1.PM (9)
样题-初级题:从考试成绩中划出及格线 10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下: (1) 及格线是10的倍数; (2) 保证至少有60%的学生及格; (3) 如果所有的学生都高于60分,则及格线为60分 样题-中级题:亮着电灯的盏数 一条长廊里依次装有n(1 ≤ n ≤ 65535)盏电灯,从头到尾编号1、2、3、…n-1、n。每盏电灯由一个拉线开关控制。开始,电灯全部关着。 有n个学生从长廊穿过。第一个学生把号码凡是1的倍数的电灯的开关拉一下;接着第二个学生把号码凡是2的倍数的电灯的开关拉一下;接着第三个学生把号码凡是3的倍数的电灯的开关拉一下;如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。 注:电灯数和学生数一致。 样题-高级题:地铁换乘 已知2条地铁线路,其中A为环线,B为东西向线路,线路都是双向的。经过的站点名分别如下,两条线交叉的换乘点用T1、T2表示。编写程序,任意输入两个站点名称,输出乘坐地铁最少需要经过的车站数量(含输入的起点和终点,换乘站点只计算一次)。 地铁线A(环线)经过车站:A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18 地铁线B(直线)经过车站:B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15 8.29去掉最大值、最小值之后剩下的个数1、输入一串数,以','分隔,输出所有数中去掉最大值、最小值之后剩下的个数。 (其中最大值与最小值可能有多个) Smple input:3,3,5,3,6,9,7,9 Sample outPut: 3 8.29从5个人中选取2个人作为礼仪 2、要从5个人中选取2个人作为礼仪,其中每个人的身高范围为160-190,要
华为上机题汇总111题
注:1.本文代码仅保证在visual studio 2005上运行通过! 2.高亮的题目为华为机试第三题左右难度。 3.本文的字符串基本运用C++里的string类,很少使用常规字符数组形式。 4.freopen()为输入重定向,方便测试,具体用法问度娘。 1.给定一个字符串,把字符串内的字母转换成该字母的下一个字母,a换成b,z换成a,Z换成A, 如aBf转换成bCg,字符串内的其他字符不改变,给定函数,编写函数 void Stringchang(const char*inpu,char*output) 其中input是输入字符串,output是输出字符串 #include #include #include using namespace std; void stringchang(const char*,char*); int main() { char input[100],output[100]; cin.getline(input,100); stringchang(input,output); cout<