本文為大家分享了華為2014筆試4道算法題,供大家參考,具體內容如下
1.通過鍵盤輸入一串小寫字母(a~z)組成的字符串。請編寫一個字符串過濾程序,若字符串中出現多個相同的字符,將非首次出現的字符過濾掉。
比如字符串“abacacde”過濾結果為“abcde”。
要求實現函數:void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
【輸入】 pInputStr: 輸入字符串
lInputLen: 輸入字符串長度
【輸出】 pOutputStr: 輸出字符串,空間已經開辟好,與輸入字符串等長;
【注意】只需要完成該函數功能算法,中間不需要有任何IO的輸入輸出
示例
輸入:“deefd” 輸出:“def”
輸入:“afafafaf” 輸出:“af”
輸入:“pppppppp” 輸出:“p”
main函數已經隱藏,這里保留給用戶的測試入口,在這里測試你的實現函數,可以調用printf打印輸出
當前你可以使用其他方法測試,只要保證最終程序能正確執行即可,該函數實現可以任意修改,但是不要改變函數原型。
一定要保證編譯運行不受影響
//////////////////////////////////////////////////////////////////////////華為第一題 19:19-19:36 17分鐘 #include <iostream> #include <cassert> using namespace std; bool g_flag[26]; void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr) { assert(pInputStr != NULL); int i = 0; if (pInputStr == NULL || lInputLen <= 1) { return; } const char *p = pInputStr; while(*p != '/0') { if (g_flag[(*p - 'a')]) { p++; }else{ pOutputStr[i++] = *p; g_flag[*p - 'a'] = 1; p++; } } pOutputStr[i] = '/0'; } int main() { memset(g_flag,0,sizeof(g_flag)); char input[] = "abacacde"; char *output = new char[strlen(input) + 1]; stringFilter(input,strlen(input),output); cout<<output<<endl; delete output; return 0; }
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”
//////////////////////////////////////////////////////////////////////////華為第二題 19:40 - 20:10 中間耽誤3分鐘 #include <iostream> #include <cassert> using namespace std; void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr) { const char *p = pInputStr; int num = 1; int i = 0; p++; while(*p != NULL) { while(*p == *(p-1)&& *p != NULL) { num++; p++; } if (num > 1) { int size = 0; int temp = num; while(num) //計算位數 { size++; num /= 10; } num = 1; for (int j = size; j > 0; j--) { pOutputStr[i+j-1] = '0'+ temp%10; temp /= 10; } i +=size; pOutputStr[i++] = *(p-1); p++; }else{ pOutputStr[i++] = *(p-1); p++; } } pOutputStr[i] = '/0'; } int main() { char input[] = "cccddecc"; char *output = new char[strlen(input) + 1]; stringZip(input,strlen(input),output); cout<<output<<endl; return 0; }
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” 注:格式錯誤
//////////////////////////////////////////////////////////////////////////華為第三題 20:29 - 20:40 #include <iostream> using namespace std; void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr) { const char *input = pInputStr; char *output = pOutputStr; int sum = 0; int operator1 = 0; int operator2 = 0; char *temp = new char[5]; char *ope = temp; while(*input != ' ') //獲得操作數1 { sum = sum*10 + (*input++ - '0'); } input++; operator1 = sum; sum = 0; while(*input != ' ') { *temp++ = *input++; } input++; *temp = '/0'; if (strlen(ope) > 1 ) { *output++ = '0'; *output = '/0'; return; } while(*input != '/0') //獲得操作數2 { sum = sum*10 + (*input++ - '0'); } operator2 = sum; sum = 0; switch (*ope) { case '+':itoa(operator1+operator2,pOutputStr,10); break; case '-':itoa(operator1-operator2,pOutputStr,10); break; default: *output++ = '0'; *output = '/0'; return; } } int main() { char input[] = "4 - 7"; char output[] = " "; arithmetic(input,strlen(input),output); cout<<output<<endl; return 0; }
4.輸入1--50個數字,求出最小數和最大數的和
//華為2014年機試題1:輸入1--50個數字,求出最小數和最大數的和 //輸入以逗號隔開 #include<stdio.h> #define N 50 void sort(int a[],int n); int main(void) { char str[100]; int a[N]={0}; gets(str); //要點1:動態的輸入1--50個整數,不能確定個數,只能用字符串輸入,然后分離出來 int i=0; int j=0; int sign=1; while(str[i]!='/0') { if(str[i]!=',') //輸入時要在半角輸入 { if(str[i] == '-') //要點:2:有負整數的輸入 { // i++; //易錯點1 sign=-1; } else if(str[i]!='/0') //不用else的話,負號也會減去‘0' { a[j]=a[j]*10 + str[i]-'0'; //要點3:輸入的可以是多位數 } } i++; if(str[i]==',' || str[i]=='/0') //這個判斷是在i自加以后 { a[j]=a[j]*sign; //易錯點2 sign=1; ////易錯點3 j++; //j就是a數組的個數 范圍0到j-1 } } sort(a,j); printf("Max number + Min number = %d",a[0]+a[j-1]); return 0; } void sort(int a[],int n) //選擇排序 { int i,j; int k; int temp; for(i=0;i<n-1;i++) { k=i; for(j=i+1;j<n;j++) { if(a[k]>a[j]) k=j; } if(i!=k) { temp = a[k]; a[k] = a[i]; a[i] = temp; } } for(i=0;i<n;i++) printf("%-5d",a[i]); puts(""); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選