生活随笔
收集整理的這篇文章主要介紹了
2014届华为校园招聘机试题2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一題、輸入一個正整數,并編碼為字符串進行輸出
描述: 1、輸入一個正整數,并編碼為字符串進行輸出。
編碼規則為:數字0-9分別編碼為字符a-j
2、輸入肯定是正整數,不用做錯誤較驗
運行時間限制: 無限制
內存限制: 無限制
輸入: 正整數
輸出: 字符串
樣例輸入: 123
樣例輸出: bcd
#include <iostream>
using namespace std;
void int2str(
int n,
char* str)
{
if (str==NULL){
return ;}
int i=
0;
char temp[
20];
while (n){temp[i++]=
'a'+n%
10;n/=
10;}temp[i]=
'\0';i--;
int j=
0;
while (i>=
0){str[j++]=temp[i--];}str[j]=
'\0';
}
void main()
{
int n;
cin>>n;
char a[
20];int2str(n,a);
cout<<a<<endl;
}
第二題、
通過鍵盤輸入一串小寫字母(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打印輸出
當前你可以使用其他方法測試,只要保證最終程序能正確執行即可,該函數實現可以任意修改,但是不要改變函數原型。一定要保證編譯運行不受影響。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;
void stringFilter(
const char *pInputStr,
long lInputLen,
char *pOutputStr)
{
if (pInputStr==NULL||lInputLen<
1){
return;}
int n=
0;
for (
int i=
0;i<lInputLen;i++){
if (i==
0){pOutputStr[n++]=pInputStr[i];}
else{
int flag=
0;
for (
int j=
0;j<n;j++){
if (pInputStr[i]==pOutputStr[j]){flag=-
1;
break;}}
if (flag==
0){pOutputStr[n++]=pInputStr[i];}}}
pOutputStr[n]=
'\0';
}
void stringFilter2(
const char *pInputStr,
long lInputLen,
char *pOutputStr)
{
bool g_flag[
26]={
0};
if (pInputStr==NULL||lInputLen<
1){
return;}
int i=
0;
while (*pInputStr!=
'\0'){
if (g_flag[*pInputStr-
'a']){pInputStr++;}
else{pOutputStr[i++]=*pInputStr;g_flag[*pInputStr-
'a']=
1;pInputStr++;}}pOutputStr[i]=
'\0';}
void stringFilter3(
const char *pInputStr,
long lInputLen,
char *pOutputStr)
{
if (pInputStr==NULL||lInputLen<
1){
return;}
string str;copy(pInputStr,pInputStr+lInputLen,back_inserter(str));sort(str.begin(),str.end());str.erase(unique(str.begin(),str.end()),str.end());
strcpy(pOutputStr,str.c_str());}
void main()
{
char *pInputStr=
"abacacde";
long lInputLen=
strlen(pInputStr);
char *pOutputStr=
new char[lInputLen+
1];stringFilter3(pInputStr,lInputLen,pOutputStr);
cout<<
"result is"<<endl;
cout<<pOutputStr<<endl;
delete[] pOutputStr;
}
總結
以上是生活随笔為你收集整理的2014届华为校园招聘机试题2的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。