题目: 题目描述(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 arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr); 【输入】 pInputStr: 输入字符串 lInputLen: 输入字符串长度 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长; 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出 示例 输入:“4 + 7” 输出:“11” 输入:“4 - 7” 输出:“-3” 输入:“9 ++ 7” 输出:“0” 注:格式错误 实现:其中第三题写的有些别扭,主要是额外的存储空间,三个函数测试通过了 #include <stdlib.h> #include <string.h> //过滤函数 void stringFilter(const char *pInputStr, char *pOutputStr) { int a[26] = {0}; const char* pstr = pInputStr; char* pResult = pOutputStr; while( *pstr != '\0') { a[*pstr - 'a']++; if(a[*pstr - 'a'] > 1) { pstr++; } else { *pResult = *pstr; pstr++; pResult++; } } *pResult = '\0'; } //压缩字符串 void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr) { const char* pFirst = pInputStr; const char* pSecond = pInputStr+1; char* pResult = pOutputStr; int count = 0; while(*pSecond != '\0') { if(*pSecond == *pFirst) { count++; } else { if(count > 0) { *pResult++ = count+1 + '0'; } *pResult++ = *pFirst; count = 0; } pFirst++; pSecond++; } if(count > 0) { *pResult++ = count+1 + '0'; } *pResult++ = *pFirst; *pResult = '\0'; } void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr) { char* pTemp = (char*)malloc(lInputLen); char* pLeft = pTemp; const char* pRight = pInputStr; while(*pInputStr != '\0') { if(*pInputStr == '+' || *pInputStr == '-') { pRight = pInputStr + 1; break; } else { *pTemp++ = *pInputStr++; } } *pTemp = '\0'; if (pRight == pLeft || *pRight == '+' || *pRight == '-') { *pOutputStr++='0'; *pOutputStr = '\0'; return; } int L = atoi(pLeft); int R = atoi(pRight); int result; switch (*pInputStr) { case '+': result = L + R; break; case '-': result = L - R; break; default: result = 0; break; } itoa(result, pOutputStr, 10); } int main() { //char a[] = "youyouare"; //char a1[] = "deefd"; //char a2[] = "afafafaf"; //char a3[] = "ppppp"; //char* b = (char*)malloc(100); //char* b1 = (char*)malloc(100); //char* b2 = (char*)malloc(100); //char* b3 = (char*)malloc(100); // //stringFilter(a,b); //stringFilter(a1,b1); //stringFilter(a2,b2); //stringFilter(a3,b3); //char t1[] = "cccddecc"; //char t2[] = "deefd"; //char t3[] = "pppppp"; //char rt1[15], rt2[15], rt3[15]; //stringZip(t1,0,rt1); //stringZip(t2,0,rt2); //stringZip(t3,0,rt3); char s1[] = "4+7"; char s2[] = "4-7"; char s3[] = "9++7"; char st1[15], st2[15], st3[15]; arithmetic(s1, strlen(s1), st1); arithmetic(s2, strlen(s2), st2); arithmetic(s3, strlen(s3), st3); } |
[技术| 编程·课件·Linux] 传说中【华为】2013校招机试的题目(纵使不真也可练习一 ...
betterzyf
· 发布于 2012-09-06 22:45
· 6737 次阅读
转载文章时务必注明原作者及原始链接,并注明「发表于 软院网 RuanYuan.Net 」,并不得对作品进行修改。