閱讀屋>面試> 去哪兒網測試面試題

去哪兒網測試面試題

去哪兒網測試面試題

1.寫一個函式,轉換相對路徑為絕對路徑,比如:/home/abs/../temp/new/../,輸出路徑為:/home/temp。

參考程式碼:

//寫一個函式,轉換相對路徑為絕對路徑,比如:/home/abs/../temp/new/../,輸出路徑為:/home/temp。

int RP2AP(const char* pInStr, char* pOutStr)

{

if (pInStr==NULL || pOutStr==NULL) return 0;

string str = pInStr;

string strTemp;

vectorvec_str;

string strOut=””;

int nPos1;

int nPos2;

nPos1 = str.find(“/”, 0);

if (nPos1<0)

{

return -1;

}

while(1)

{

nPos2 = str.find(“/”, nPos1+1);

if (nPos2>nPos1)

{

strTemp = str.substr(nPos1, nPos2-nPos1);

//如果不是/..,就放入vector裡

if (strTemp!=”/..”)

vec_str.push_back(strTemp);

else//彈出上一個

{

vec_str.reserve(vec_str.size());

vec_str.pop_back();

vec_str.reserve(vec_str.size());

}

nPos1 = nPos2;

}

else

{

break;

}

}

//迴圈賦值累加

for (int i=0; i<vec_str.size(); p="" i++)<="">

{

strOut +=vec_str[i];

}

//這裡用strOut.c_str(),要安全一些,有的環境不這樣寫編譯都不過。

memcpy(pOutStr, strOut.c_str(), strOut.size());

return 1;

}

int main()

{

int n = 10;

//char a[] = “/home/abs/../temp/new/../”;

char a[] = “/home/abs/temp/new/../”;

char b[256];

memset(b, 0, 256);

int nRet = RP2AP(a, b);

if (nRet ==1 )

cout << b << endl;

system(“pause”);

return 0;

} //寫一個函式,轉換相對路徑為絕對路徑,比如:/home/abs/../temp/new/../,輸出路徑為:/home/temp。

int RP2AP(const char* pInStr, char* pOutStr)

{

if (pInStr==NULL || pOutStr==NULL) return 0;

string str = pInStr;

string strTemp;

vectorvec_str;

string strOut=””;

int nPos1;

int nPos2;

nPos1 = str.find(“/”, 0);

if (nPos1<0)

{

return -1;

}

while(1)

{

nPos2 = str.find(“/”, nPos1+1);

if (nPos2>nPos1)

{

strTemp = str.substr(nPos1, nPos2-nPos1);

//如果不是/..,就放入vector裡

if (strTemp!=”/..”)

vec_str.push_back(strTemp);

else//彈出上一個

{

vec_str.reserve(vec_str.size());

vec_str.pop_back();

vec_str.reserve(vec_str.size());

}

nPos1 = nPos2;

}

else

{

break;

}

}

//迴圈賦值累加

for (int i=0; i<vec_str.size(); p="" i++)<="">

{

strOut +=vec_str[i];

}

//這裡用strOut.c_str(),要安全一些,有的環境不這樣寫編譯都不過。

memcpy(pOutStr, strOut.c_str(), strOut.size());

return 1;

}

int main()

{

int n = 10;

//char a[] = “/home/abs/../temp/new/../”;

char a[] = “/home/abs/temp/new/../”;

char b[256];

memset(b, 0, 256);

int nRet = RP2AP(a, b);

if (nRet ==1 )

cout << b << endl;

system(“pause”);

return 0;

}

2.一個10*10的矩陣(可以理解為棋盤),隨時生成一組資料填入矩陣,任何一個位置的數字除4進行計算,按餘數著色,餘數為0著色為red,1為blue,2為green,3為black,可以理解為生成4中顏色的棋子放入棋盤,如果存在其中同色五星連珠的.情況(規則通五子棋),找出任意一組,輸出5個棋子的位置下標值。

3.

有兩個檔案context.txt和words.conf,請嘗試將他們合併成為一段文字,並打印出來。

這兩個檔案內容如下:

context.txt

“並不是每個人都需要$(qunar)自己的糧食,$(flight.1)每個人都需要做自己穿的$(flight.2),我們說著別人發明的$(hotel),使用別人發明的數學……我們一直在$(tuan)別人的成果。使用人類的已有經驗和知識$(travel.1)來進行,是一件$(travel.2)的事情”

word.conf

flight=也不是:衣服

qunar=種植

hotel=語言

tuan=使用

travel=發明創造:很了不起

4.

一個檔案裡有10萬個隨機正整數,按照以下規則能組合出一份新的資料:

A. 如果當前數字能被3整除,那麼它和檔案中所有數字(包括自己)兩兩相加後生成一組數字替代自己的位置。

B. 如果不能被3整除,則它只需要乘以二,生成一個數字替代自己的位置。

例如:[3,7,6] 會組合出[6,10,9,14,9,13,12]

再如:[5,12,9,6,2]會組合出[10,17,24,21,18,14,14,21,18,15,11,11,18,15,12,8,4]

寫一個程式找出並打印出新資料的最小的前200個數字。請考慮最佳化演算法複雜度。

5.已知字母序列【d, g, e, c, f, b, o, a】,請實現一個函式針對輸入的一組字串 input[] = {“bed”, “dog”, “dear”, “eye”},按照字母順序排序並列印。

本例的輸出順序為:dear, dog, eye, bed。

6.有一萬個北京單身男女向你提交了基本資料,包括:姓名、性別、年齡、星座,寫一段程式嘗試找出他們最匹配的一對。


[去哪兒網測試面試題]相關文章:

1.去哪兒網測試面試題

【去哪兒網測試面試題】相關文章: