华为2014校招机试题

      题目来自网络和师兄口述提供,转载请注明出处,谢谢!

1.通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。

压缩规则:
    1、仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc"。
    2、压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"。

要求实现函数: 
   void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
    输入pInputStr:  输入字符串lInputLen:  输入字符串长度
    输出 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

注意:只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例 
    输入:“cccddecc”   输出:“3c2de2c”
    输入:“adef”     输出:“adef”
    输入:“pppppppp” 输出:“8p”

 1 #include"stdafx.h"
 2 #include<iostream>
 3 using namespace std;
 4 
 5 #define MAX 100
 6 
 7 void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
 8 {
 9     int i;
10     int j=0;
11     int site=0;
12     for(i=0;i<lInputLen;i++)
13     {
14         if(pInputStr[i]!=pInputStr[i+1])
15         {
16             if(i==site)
17             {
18                 pOutputStr[j]=pInputStr[i];
19                 j++;
20                 site=i+1;
21             }
22             else
23             {
24                 int num=i-site+1;
25                 char ch[MAX];
26                 memset(ch,0,sizeof(ch));
27                 itoa(num,ch,10);
28                 strcpy(pOutputStr+j,ch);
29                 j+=strlen(ch);
30                 pOutputStr[j]=pInputStr[i];
31                 j++;
32                 site=i+1;
33             }
34         }        
35     }
36 }
37 
38 int main()
39 {
40     char p[MAX]={0};
41     while(cin>>p)
42     {
43         char q[MAX]={0};
44         stringZip(p,strlen(p),q);
45         cout<<q<<endl;
46     }
47     return 0;
48 }

      此方法时间复杂度仅为O(n),只需遍历一次数组。

      有一个问题,若重复字符达到10+,怎么输出?题目没说明,所以代码中也没有考虑这个问题。(已解决,10.02 update)

 

2.通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。 输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

补充说明:
1、操作数为正整数,不需要考虑计算结果溢出的情况。
2、若输入算式格式错误,输出结果为“0”。

要求实现函数: void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:  输入字符串             lInputLen:  输入字符串长度        
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
 
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例

输入:“4 + 7”  输出:“11”
输入:“4 - 7”  输出:“-3”
输入:“9 ++ 7”  输出:“0” 注:格式错误

 1 #include"stdafx.h"
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 
 6 #define MAX 100
 7 
 8 void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
 9 {
10     int num1,num2,sum;
11     char *ch1=new char[lInputLen+1];
12     char ch2;
13     char *ch3=new char[lInputLen+1];
14     bool flag=true;
15     int i=0;
16     int j=0;
17     while(pInputStr[i]!=' ')
18     {
19         ch1[i]=pInputStr[i];
20         i++;
21     }
22     ch1[i]='';
23     ch2=pInputStr[++i];
24 
25     if(pInputStr[++i]!=' ')
26         flag=false;
27 
28     i++;
29     while(pInputStr[i]!='')
30     {
31         ch3[j]=pInputStr[i];
32         i++;
33         j++;
34     }
35     ch3[j]='';
36 
37     for(i=0;i<strlen(ch1);++i)
38     {
39         if(ch1[i]>'9'||ch1[i]<'0')
40             flag=false;
41     }
42     if(ch2!='+'&&ch2!='-')
43         flag=false;
44     for(i=0;i<strlen(ch3);++i)
45     {
46         if(ch3[i]>'9'||ch3[i]<'0')
47             flag=false;
48     }
49 
50     if(flag)
51     {
52         num1=atoi(ch1);
53         num2=atoi(ch3);
54         if(ch2=='+') sum=num1+num2;
55         if(ch2=='-') sum=num1-num2;
56         itoa(sum,pOutputStr,10);
57     }
58     else
59     {
60         pOutputStr[0]='0';
61         pOutputStr[1]='';
62     }
63 }
64 
65 int main()
66 {
67     char p[MAX];
68     cin.get(p,MAX);
69     char q[MAX];
70     memset(q,0,sizeof(q));
71     arithmetic(p,strlen(p),q);
72     cout<<q<<endl;    
73     return 0;
74 }

输入一串字符串,其中有普通的字符与括号组成(包括‘(’、‘)’、‘[’,']'),要求验证括号是否匹配,如果匹配则输出0、否则输出1.

样例:
输入:dfa(sdf)df[dfds(dfd)]   
输出:0

 1 int IsMatching(const char *pInputStr)
 2 {
 3     stack<char> s;
 4     int i;
 5     int answer=1;
 6     for(i=0;i<strlen(pInputStr);i++)
 7     {
 8         if(pInputStr[i]=='('||pInputStr[i]=='[')
 9             s.push(pInputStr[i]);
10         if(s.size()>0&&(pInputStr[i]==')'||pInputStr[i]==']'))
11         {
12             if(pInputStr[i]==')'&&s.top()=='(')
13                 s.pop();
14             else if(pInputStr[i]==']'&&s.top()=='[')
15                 s.pop();
16             else
17             {
18                 answer=0;
19                 break;
20             }
21         }
22     }
23     if(s.size()!=0) answer=0;
24     return answer;
25 }

4.输入两个字符串(都是字母),a到z每个字母有一个权值(1-26),不区分大小,哪个字母分配哪个权值由你决定,字符串的权值就是字符串中所有字母的权值之和,现要求自行分配权值,使得两个字符串的权值之差最大,输出最大权值之差。

      总结:总的来说,华为上机题不难,但是能否在规定时间内真的又快又好得做对给的3题?还是要多加练习,任道重远...

              另外平时还是要练习手写程序,毕竟只有华为采用了上机模式。

      如有错误,欢迎批评指正!

原文地址:https://www.cnblogs.com/Rosanna/p/3347777.html