2014校园招聘华为机试题:
1、通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数格式:void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
代码如下:
class Solution{
public:
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr){
int hash[26] = {0};
int j = 0;
for(int i = 0; i < lInputLen; i++){
hash[*pInputStr - 'a'] ++;
cout << *pInputStr << "的hash: " << hash[*pInputStr - 'a'] << endl;
if(hash[*pInputStr - 'a'] == 1){
pOutputStr[j++] = *pInputStr;
}
pInputStr++;
}
pOutputStr[j] = '\0';
cout << pOutputStr << endl;
}
};
测试代码:
int main(){
const char* input = "abacacde";
long len = strlen(input);
char* output = (char*)malloc(sizeof(char) * (len + 1));
Solution s;
s.stringFilter(input, len, output);
}
2、通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串(重复次数+字母)。
函数格式:void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
代码如下:
class Solution2{
public:
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr){
int j = 0;
int cnt = 1;
for(int i = 1; i <= lInputLen ; ){
while(pInputStr[i] == pInputStr[i - 1]){
i++;
cnt++;
}
cout << cnt << endl;
if(cnt == 1){
pOutputStr[j++] = pInputStr[i - 1];
}
else{ //如果出现次数大于1次,有可能10以上次数,所以得处理
int size = 0;
char tmp[100];
while(cnt > 0){
tmp[size++] = cnt % 10 + '0';
cnt /= 10;
}
for(int k = size - 1; k >= 0; k--){
pOutputStr[j++] = tmp[k];
}
pOutputStr[j++] = pInputStr[i - 1];
cnt = 1;
}
i++;
}
pOutputStr[j] = '\0';
cout << pOutputStr << endl;
}
};
测试代码:
int main(){
const char* input = "aaabacccacddddddddddddee";
char* output = (char*)malloc(sizeof(char) * (strlen(input) + 1));
Solution2 s;
s.stringZip(input, strlen(input), output);
}
3、通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。如果输入格式错误,则输出0
函数格式:void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);
代码如下:
<pre name="code" class="cpp">class Solution3{
public:
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr){
char a[100]; //保存第一个数字的字符串
char b[100]; //保存第二个数字的字符串
char c;
int sum;
int i = 0, j = 0;
while(*pInputStr != ' ' && *pInputStr != '\0'){
if((*pInputStr - '0' ) > 9 || (*pInputStr - '0' ) < 0){
cout << "第一个数字输入错误" << endl;
cout << 0 << endl;
exit(0);
}
a[i++] = *pInputStr;
pInputStr++;
}
a[i] = '\0';
int aa = atoi(a);
cout << "aa = " << aa << endl;
pInputStr++; //遇到空格后移动到空格后的一个字符,空格后应该检查是否为正负号
if((*pInputStr != '+') && (*pInputStr != '-')){
cout << "+-输入错误" << endl;
cout << 0 << endl;
exit(0);
}else{
c = *pInputStr++; //此时指针++后指向符号后面的空格
cout << "c = " << c << endl;
}
if(*pInputStr++ != ' '){//正负号后,应该检查是否再有一个空格
cout << "空格输入错误" << endl;
cout << 0 << endl;
exit(0);
}
while(*pInputStr != '\0'){
if((*pInputStr - '0' ) > 9 || (*pInputStr - '0' ) < 0){
cout << "第二个数字输入错误" << endl;
cout << 0 << endl;
exit(0);
}
b[j++] = *pInputStr;
pInputStr++;
}
b[j] = '\0';
int bb = atoi(b);
cout << "bb = " << bb << endl;
if(c == '+'){
sum = aa + bb;
}
else{
sum = aa- bb;
}
cout << sum << endl;
itoa(sum, pOutputStr, 10);
cout << "输出的字符串为: " << pOutputStr << endl;
}
};
测试代码:
int main(){
const char* input = "100 - 52";
char output[100];
Solution3 s;
s.arithmetic(input, 9, output);
}