1. 程式人生 > >招銀網路筆試總結

招銀網路筆試總結

又被虐了,可惡的是它居然還出了幾道java,jvm,web,xml的題。雖然心裡面已經又準備,但心裡無疑又增添了不少緊迫感。無論如何,一直總結才是前進的王道,加油呀!

先來回顧一道程式設計題,講道理,這道程式設計題出得好爛啊,不知所云,也無法測試,而且返回什麼也不說清楚。題目說,招行網路要去外面拿訂單,有三個辦公室,每個辦公室有自己的權重:
JB01 1;
JB02 2;
JB03 1;
而且這些辦公室拿一輪訂單就結束了,剩下的訂單可以先拋棄。要你寫一個函式,實參一個訂單的ID列表(我就姑且認為是順序容器vector吧)和map的權重表,這個函式返回每個辦公室的訂單表。

#include <vector>
#include <iostream> #include <string> #include <map> using namespace std; void allocate(vector<string> ID, map<string,int> weight, map<string,vector<string>> &temp){ int i=0; for(auto &w:weight){ vector<string> t; while
(w.second){ t.push_back(ID[i++]); --w.second; } temp.insert(make_pair(w.first,t)); } } int main(){ map<string,int> weight{{"JB01",1},{"JB02",2},{"JB03",1}}; vector<string> ID{"11","22","33","44","55"}; string word; map<string,vector
<string>
>
temp; allocate(ID,weight,temp); cout<<"Please input your office:"<<endl; cin>>word; map<string,vector<string>>::iterator iter=temp.find(word); if(iter!=temp.end()) { vector<string>::iterator it = iter->second.begin(); while(it != iter->second.end()) { cout<<(*it++)<<endl; } } return 0; }

這個題目涉及到了map巢狀vector的示例(我個人認為),關聯容器有很多靈活的使用方法,先來回顧一下關聯容器的insert成員:向容器中新增一個元素的四種方法

map<string,size_t> word_count;
word_count.insert({word,1});
word_count.insert(make_pair(word,1));
word_count.insert(pair<string,size_t>)(word,1));
word_count.insert(map<string,size_t>::value_type(word,1));

解法中,我直接使用了make_pair去insert成員。在這裡,map巢狀vector的關鍵其實是要將主鍵和已經建立好的一組向量去make_pair,而不能直接去push_back(),我前期也犯了很大的錯誤。其次,我們要用迭代器作為指標,然後解指標獲得map的值。