今天下午五点半参加了华为的软件工程师上机考试,用的是C,给定时间1小时15分钟。下面把题目贴出来~
1.题目描述(60分):
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数:
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“deefd” 输出:“def”
输入:“afafafaf” 输出:“af”
输入:“pppppppp” 输出:“p”
代码如下,欢迎纠错:
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
int i;
int j;
int ac[256] = {0};
if (lInputLen == 0)
{
pOutputStr[0] = 0;
}
for (i = 0, j = 0; i < lInputLen; i ++)
{
if (0 == ac[pInputStr[i]])
{
pOutputStr[j] = pInputStr[i];
ac[pInputStr[i]] = 1;
j ++;
}
}
pOutputStr[j] = 0;
}
2.题目描述(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”
代码如下,欢迎纠错:
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
int i;
int j;
int iCount;
int iOutputNum;
int iTmp;
int iNumBits;
char acNum[16];
for (i = 0, iOutputNum = 0; i < lInputLen; i = i + j)
{
for (j = 1, iCount = 0; i + j < lInputLen; j ++, iCount ++)
{
if (pInputStr[i + j] != pInputStr[i])
{
break;
}
}
/* 数字转换字符串,并填入输出字符串数组 */
if (iCount > 0)
{
iTmp = 0;
iNumBits = 0;
iCount = iCount + 1;
while (iCount > 0)
{
iTmp = iCount % 10;
iCount /= 10;
acNum[iNumBits] = iTmp + '0';
iNumBits ++;
}
/* 要考虑重复次数超过9 */
while (iNumBits > 0)
{
pOutputStr[iOutputNum] = acNum[iNumBits - 1];
iNumBits --;
iOutputNum ++;
}
}
pOutputStr[iOutputNum] = pInputStr[i];
iOutputNum ++;
}
pOutputStr [iOutputNum] = 0;
}
3.题目描述(50分):
通过键盘输入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” 注:格式错误
代码如下,欢迎纠错:
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
char *pc;
char *pcEnd;
char cOper;
char acszNum[16];
int iTmpNum;
int iNum;
int iPos;
pc = (char *) pInputStr;
pcEnd = (char *) pInputStr + lInputLen;
cOper = '+';
iNum = 0;
while (pc < pcEnd)
{
/* 读数字 */
iTmpNum = 0;
while (*pc > '0' && *pc <= '9' && pc < pcEnd)
{
iTmpNum = iTmpNum * 10 + (*pc - '0');
pc ++;
}
if (0 == iTmpNum)
{
break;
}
if ('+' == cOper)
{
iNum += iTmpNum;
}
else if ('-' == cOper)
{
iNum -= iTmpNum;
}
else
{
break;
}
if (*pc != ' ' && *(pc + 2) != ' ')
{
break;
}
else
{
cOper = *(pc + 1);
pc += 3;
}
}
if (pc < pcEnd)
{
pOutputStr[0] = '0';
pOutputStr[1] = 0;
return;
}
iPos = 0;
while (iNum != 0)
{
acszNum[iPos] = iNum % 10 + '0';
iNum /= 10;
iPos ++;
}
while (iPos > 0)
{
*pOutputStr = acszNum[iPos - 1];
pOutputStr ++;
iPos --;
}
*pOutputStr = 0;
}
欢迎大家以评论的方式积极讨论。另外,我已经把面试题的工程文件上传到我的资源里,0积分,欢迎下载。