zoukankan      html  css  js  c++  java
  • 华为2012.09.03浙大机试题

    1、

    通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
    比如字符串“abacacde”过滤结果为“abcde”。
     
    要求实现函数: 
    void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
     
    【输入】 pInputStr:  输入字符串
             lInputLen:  输入字符串长度         
    【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
     
    【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
     
    示例 
    输入:“deefd”        输出:“def”
    输入:“afafafaf”     输出:“af”
    输入:“pppppppp”     输出:“p”
     
     
    2、
    通过键盘输入一串小写字母(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”
     
     
    3、
    通过键盘输入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” 注:格式错误
     
     
    参考程序(欢迎讨论) 转载请注明来源http://www.cnblogs.com/jerry19880126/
     
      1 #include <iostream>
      2 using namespace std;
      3 
      4 // 用于测试结果
      5 void print(char *pOutputStr)
      6 {
      7     char *p = pOutputStr;
      8     while(p != NULL && *p != 0)
      9     {
     10         cout << *p ;
     11         ++p;
     12     }
     13     cout << endl;
     14 }
     15 
     16 /************************************************************************/
     17 /*
     18 编程题第一题
     19 */
     20 /************************************************************************/
     21 /*
     22 【输入】 pInputStr:  输入字符串
     23 lInputLen:  输入字符串长度         
     24 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
     25 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
     26 */
     27 void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
     28 {
     29     bool table[26] = {false};
     30     long k = 0;
     31     for(long i = 0; i < lInputLen; ++i)
     32     {
     33         char c = pInputStr[i];
     34         if(table[c-'a'] == false)
     35         {
     36             pOutputStr[k++] = c;
     37             table[c-'a'] = true;
     38         }
     39     }
     40     pOutputStr[k] = 0;
     41 }
     42 
     43 
     44 
     45 /************************************************************************/
     46 /* 
     47 编程题第二题
     48 */
     49 /************************************************************************/
     50 /*
     51 【输入】 pInputStr:  输入字符串
     52 lInputLen:  输入字符串长度         
     53 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
     54 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
     55 */
     56 
     57 void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
     58 {
     59     long repeat = 1;
     60     long k = 0;
     61     for(long i = 0; i < lInputLen; ++i)
     62     {
     63         char c = pInputStr[i];
     64         if(i + 1 < lInputLen && c == pInputStr[i + 1])
     65         {
     66             ++ repeat;
     67         }
     68         else
     69         {
     70             // 写入
     71             if(repeat != 1)
     72             {    
     73                 // 当心repeat超过9的情况
     74                 
     75                 char temp[100];
     76                 itoa(repeat, temp, 10);
     77                 int ii = 0;
     78                 while(temp[ii])
     79                 {
     80                     pOutputStr[k++] = temp[ii++];
     81                 }        
     82             }
     83             pOutputStr[k++] = c;
     84             repeat = 1;
     85         }
     86     }
     87     pOutputStr[k] = 0;
     88 }
     89 
     90 /************************************************************************/
     91 /*
     92 编程题第三题
     93 */
     94 /************************************************************************/
     95 /*
     96 【输入】 pInputStr:  输入字符串
     97 lInputLen:  输入字符串长度         
     98 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
     99 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
    100 */
    101 void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
    102 {
    103     pOutputStr[0] = '0';
    104     pOutputStr[1] = 0;
    105     if(!pInputStr)
    106     {
    107         return;
    108     }
    109     // 获得第一个操作数
    110     int operand1 = 0;
    111     long index = 0;
    112     while(pInputStr[index] >= '0' && pInputStr[index] <= '9')
    113     {
    114         operand1 = operand1 * 10 + (pInputStr[index++] - '0');
    115         if(index >= lInputLen) return;
    116     }
    117     if(pInputStr[index++] != ' ' || index >= lInputLen)
    118     {
    119         return;
    120     }
    121     // 获得操作符
    122     char oper;
    123     if(pInputStr[index] == '+' || pInputStr[index] == '-')
    124     {
    125         oper = pInputStr[index++];
    126     }
    127     else
    128     {
    129         return;
    130     }
    131     if(index >= lInputLen || pInputStr[index++] != ' ')
    132     {
    133         return;
    134     }
    135     if(index >= lInputLen) return;
    136     // 获得第二个操作数
    137     int operand2 = 0;
    138     while(pInputStr[index] >= '0' && pInputStr[index] <= '9')
    139     {
    140         operand2 = operand2 * 10 + (pInputStr[index++] - '0');
    141         if(index >= lInputLen) return;
    142     }
    143     if(pInputStr[index] != 0)
    144     {
    145         return;
    146     }
    147     // 输入都是合法的
    148     int result;
    149     switch(oper)
    150     {
    151     case '+':
    152         result = operand1 + operand2;
    153         break;
    154     case '-':
    155         result = operand1 - operand2;
    156         break;
    157     }
    158     int k = 0;
    159     if(result < 0)
    160     {
    161         result = -result;
    162         pOutputStr[k++] = '-';
    163     }
    164     itoa(result, pOutputStr + k, 10);
    165 }
    166 
    167 int main()
    168 {
    169     // 第一题测试样例
    170     char output1[100], output2[100], output3[100];
    171     char *input1 = "deefd";
    172     stringFilter(input1, strlen(input1) + 1, output1);
    173     print(output1);
    174     char *input2 = "afafafaf" ;
    175     stringFilter(input2, strlen(input2) + 1, output2);
    176     print(output2);
    177     char *input3 = "pppppppp" ;
    178     stringFilter(input3, strlen(input3) + 1, output3);
    179     print(output3);
    180     
    181 
    182     // 第二题测试样例
    183     char output21[100], output22[100], output23[100];
    184     char *input21 = "aaaaaaaaaaaaaaaaaaaaaaaaabbcd";
    185     stringZip(input21, strlen(input21) + 1, output21);
    186     print(output21);
    187     char *input22 = "cccddecc";
    188     stringZip(input22, strlen(input22) + 1, output22);
    189     print(output22);
    190     char *input23 = "8p";
    191     stringZip(input23, strlen(input23) + 1, output23);
    192     print(output23);
    193 
    194     // 第三题测试样例
    195     char output31[100], output32[100], output33[100];
    196     char *input31 = "4 + 7";
    197     arithmetic(input31, strlen(input31) + 1, output31);
    198     print(output31);
    199     char *input32 = "4 - 7";
    200     arithmetic(input32, strlen(input32) + 1, output32);
    201     print(output32);
    202     char *input33 = "9 ++ 7";
    203     arithmetic(input33, strlen(input33) + 1, output33);
    204     print(output33);
    205 }
    注:题目转自http://blog.csdn.net/luno1/article/details/7945227,解答不同,可以都去参考一下。
     
    第三题没有考虑到index可以越界(大于等于lInputLen的情况),现在下面补上
     
     1 void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
     2 {
     3     pOutputStr[0] = '0';
     4     pOutputStr[1] = 0;
     5     if(!pInputStr)
     6     {
     7         return;
     8     }
     9     // 获得第一个操作数
    10     int operand1 = 0;
    11     long index = 0;
    12     while(pInputStr[index] >= '0' && pInputStr[index] <= '9')
    13     {
    14         operand1 = operand1 * 10 + (pInputStr[index++] - '0');
    15         if(index >= lInputLen) return;
    16     }
    17     if(pInputStr[index++] != ' ' || index >= lInputLen)
    18     {
    19         return;
    20     }
    21     // 获得操作符
    22     char oper;
    23     if(pInputStr[index] == '+' || pInputStr[index] == '-')
    24     {
    25         oper = pInputStr[index++];
    26     }
    27     else
    28     {
    29         return;
    30     }
    31     if(index >= lInputLen || pInputStr[index++] != ' ')
    32     {
    33         return;
    34     }
    35     if(index >= lInputLen) return;
    36     // 获得第二个操作数
    37     int operand2 = 0;
    38     while(pInputStr[index] >= '0' && pInputStr[index] <= '9')
    39     {
    40         operand2 = operand2 * 10 + (pInputStr[index++] - '0');
    41         if(index >= lInputLen) return;
    42     }
    43     if(pInputStr[index] != 0)
    44     {
    45         return;
    46     }
    47     // 输入都是合法的
    48     int result;
    49     switch(oper)
    50     {
    51     case '+':
    52         result = operand1 + operand2;
    53         break;
    54     case '-':
    55         result = operand1 - operand2;
    56         break;
    57     }
    58     int k = 0;
    59     if(result < 0)
    60     {
    61         result = -result;
    62         pOutputStr[k++] = '-';
    63     }
    64     itoa(result, pOutputStr + k, 10);
    65 }
     
  • 相关阅读:
    跟我一起学extjs5(02--建立project项目)
    SVN经常使用操作
    CWnd::Attach()具体解释
    51nod1160 压缩算法的矩阵——一道有趣的题
    Java实现 LeetCode 594 最长和谐子序列(滑动窗口)
    Java实现 LeetCode 1162 地图分析(可以暴力或者动态规划的BFS)
    Java实现 LeetCode 1162 地图分析(可以暴力或者动态规划的BFS)
    Java实现 LeetCode 1162 地图分析(可以暴力或者动态规划的BFS)
    Java实现 LeetCode 593 有效的正方形(判断正方形)
    Java实现 LeetCode 593 有效的正方形(判断正方形)
  • 原文地址:https://www.cnblogs.com/jerry19880126/p/2676291.html
Copyright © 2011-2022 走看看