您的位置:首页 > 编程语言 > C语言/C++

华为2014机试

2014-03-27 17:11 274 查看
华为2014年机试题目:

题一:题目描述(60分):

通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。

比如字符串“abacacde”过滤结果为“abcde”。

要求实现函数: 

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:  输入字符串

         lInputLen:  输入字符串长度         

【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

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

示例 

输入:“deefd”        输出:“def”

输入:“afafafaf”     输出:“af”

输入:“pppppppp”     输出:“p”

题二:题目描述(40分):

通过键盘输入一串小写字母(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”

 

题三:题目描述(50分): 

通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。

输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

补充说明:

1. 操作数为正整数,不需要考虑计算结果溢出的情况。

2. 若输入算式格式错误,输出结果为“0”。

自己动手实践了下,代码如下,欢迎纠错~

题目一:

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
vector<char> temp;
int * a = new int[26];
while (*pInputStr != NULL)
{
if (temp.empty())
{
temp.push_back(*pInputStr);
a[*pInputStr - 'a'] = 1;
}

else
{
if (!a[*pInputStr - 'a'])
{
temp.push_back(*pInputStr);
a[*pInputStr - 'a'] = 1;
}
}
pInputStr++;

}
for (vector<char>::iterator iter = temp.begin(); iter != temp.end(); ++iter)
{
*pOutputStr = *iter;
pOutputStr++;
}

}
题目二:

void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
vector<char> temp1;
vector<int> temp2;
int cnt = 0;
while (*pInputStr != NULL)
{
if (temp1.empty())
{
temp1.push_back(*pInputStr);
cnt = 1;
}
else
{
char temp = *(pInputStr - 1);
if (*pInputStr == temp)
{
cnt++;

}
else
{
temp2.push_back(cnt);
cnt = 1;
temp1.push_back(*pInputStr);
}
}
pInputStr++;
}
temp2.push_back(cnt);
vector<char>::iterator iter1 = temp1.begin();
vector<int>::iterator iter2 = temp2.begin();

for (iter1 = temp1.begin(), iter2 = temp2.begin(); iter1 != temp1.end() && iter2 != temp2.end(); iter1++, iter2++)
{
string s;
int i = *iter2;;
do
{
char c = i % 10 +'0';
s = s + c;
i = i / 10;
}while (i);
if (s == "1")
{
*pOutputStr = *iter1;
}
else
{
for (int j = s.length() - 1; j >= 0; j--)
{
*pOutputStr = s[j];
pOutputStr++;
}
*pOutputStr = *iter1;
}

pOutputStr++;
}

题目三:

void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
string s1;
string s2;
string s3;

int cnt = 1;

while (*pInputStr != NULL)
{
if (*pInputStr != ' ')
{
if (cnt==1)
s1 = s1 + *pInputStr;
if (cnt==2)
s2 = s2 + *pInputStr;
if (cnt == 3)
s3 = s3 + *pInputStr;

}
else
{
cnt++;
}
pInputStr++;
}
int len1 = s1.length();
int len2 = s2.length();
int len3 = s3.length();
int operator1 = 0;
int operator2 = 0;
for (int i = 0; i < len1; i++)
{
operator1 = operator1 + (s1[i] - '0')* pow(10, len1 - 1 - i);
}
for (int i = 0; i < len3; i++)
{
operator2 = operator2 + (s3[i] - '0')* pow(10, len3 - 1 - i);
}
int result = 0;
if (s2.length()>1)
result = 0;
else
{
if (s2 == "+")
result = operator1 + operator2;
else
result = operator1 - operator2;
}
string daan;
if (result > 0)
{
int i = result;
do
{
char c = i % 10 + '0';
daan = daan + c;
i = i / 10;
} while (i);
}
else if(result < 0)
{
int i = result*-1;
do
{
char c = i % 10 + '0';
daan = daan + c;
i = i / 10;
} while (i);
daan = daan + "-";
}
else
{
daan = '0';
}
for (int i = daan.length() - 1; i >= 0; i--)
{
*pOutputStr = daan[i];
pOutputStr++;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++语言基础