ThoughtWorks 2018校園招聘作業題目 -- 計程車
阿新 • • 發佈:2018-12-31
題目描述
題目僅供技術交流,未經公司允許,不得傳播。
題目分析
這道題本身不難,按照保養規則模擬即可。
但是,對於測試用例二的CAR0001,和CAR0003,根據題目中報廢規則,筆者認為屬於“報廢”。但給出的輸出沒有包含這兩輛車。
如下圖所示:
參考程式碼
#include <bits/stdc++.h>
using namespace std;
int submitYear, submitMonth, submitDay;
unordered_map<string, vector<string>> umap;
void splitString(const std::string& s, std::vector<std::string>& v, const std::string& c) {
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while(std::string::npos != pos2) {
v.push_back(s.substr(pos1, pos2-pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
}
vector<int> getDate(string str, vector<string>& vstring) {
vector<int> res;
splitString(str, vstring, "/");
res.push_back(stoi(vstring[0]));
res.push_back(stoi(vstring[1]));
res.push_back(stoi(vstring[2 ]));
return res;
}
void carsMaintain(string str) {
string carNumber;
int produceYear, produceMonth, produceDay;
int ageYear, ageMonth, ageDay;
string company;
int milesLength;
bool isFixed = false;
vector<string> vecString;
vector<string> vecProduceDate;
splitString(str, vecString, "|");
splitString(vecString[1], vecProduceDate, "/");
carNumber = vecString[0];
produceYear = stoi(vecProduceDate[0]);
produceMonth = stoi(vecProduceDate[1]);
produceDay = stoi(vecProduceDate[2]);
ageYear = submitYear - produceYear;
ageMonth = submitMonth - produceMonth; // Becareful the minus number
ageDay = submitDay - produceDay;
company = vecString[2];
milesLength = stoi(vecString[3]);
isFixed = vecString[4] == "T";
// write off
if (ageYear >= 6 || (ageYear >= 3 && isFixed)) {
if (ageMonth <= 1) {
umap["Write-Off"].push_back(str);
}
}
// Distance related
else if ((10000 - milesLength % 10000 <= 500) || milesLength %10000 == 0) {
umap["Distance-Related"].push_back(str);
}
// Time related
else {
// has been fixed
if (isFixed) {
if (ageYear >= 1) {
if ((12 - produceMonth + submitMonth) % 3 != 1) {
umap["Time-Related"].push_back(str);
}
}
else {
if (ageMonth % 3 != 1) {
umap["Time-Related"].push_back(str);
}
}
}
else if (ageYear >= 3) {
if ((12 - produceMonth + submitMonth) % 6 == 0 || (12 - produceMonth + submitMonth) % 6 == 5) {
umap["Time-Related"].push_back(str);
}
}
else {
if ((12 - produceMonth + submitMonth) % 12 == 0 || (12 - produceMonth + submitMonth) % 12 == 11) {
umap["Time-Related"].push_back(str);
}
}
}
}
map<string, vector<string>> printHelper(vector<string>& vecString) {
map<string, vector<string>> mmap;
for (auto str : vecString) {
vector<string> vstr;
splitString(str, vstr, "|");
mmap[vstr[2]].push_back(vstr[0]);
}
return mmap;
}
void printFormat(vector<string> vstr) {
auto mmap = printHelper(vstr);
for (auto item : mmap) {
cout << item.first << ": " << item.second.size() << " ";
for (int i = 0; i < item.second.size(); ++i) {
if (i == 0) {
if (i == item.second.size() - 1) cout << "(" << item.second[i] << ")";
else cout << "(" << item.second[i] << ",";
}
else if (i == item.second.size() - 1 ) cout << " " << item.second[i] << ")";
else cout << " " << item.second[i] << ",";
}
cout << endl;
}
}
void printOut() {
cout << "Reminder" << endl;
cout << "==================" << endl;
cout << endl;
cout << "* Time-related maintenance coming soon..." << endl;
printFormat(umap["Time-Related"]);
cout << endl << "* Distance-related maintenance coming soon..." << endl;
printFormat(umap["Distance-Related"]);
cout << endl << "* Write-off coming soon..." << endl;
printFormat(umap["Write-Off"]);
}
int main() {
freopen("C:\\Users\\Administrator\\Desktop\\ClionTest\\taxi.in", "r", stdin);
freopen("C:\\Users\\Administrator\\Desktop\\ClionTest\\taxi.out", "w", stdout);
// get submit date from the first line
string firstLine, submitDate;
vector<string> vstring;
getline(cin, firstLine);
submitDate = firstLine.substr(firstLine.find(' ') + 1);
vector<int> submitDateVec = getDate(submitDate, vstring);
submitYear = submitDateVec[0];
submitMonth = submitDateVec[1];
submitDay = submitDateVec[2];
// process the following lines
string str;
while (getline(cin, str)) {
carsMaintain(str);
}
// print the results
printOut();
return 0;
}