華為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 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM