ThoughtWorks 2018校园招聘作业题目 -- 出租车

题目描述

题目仅供技术交流,未经公司允许,不得传播。

ThoughtWorks 2018校园招聘作业题目 -- 出租车_第1张图片
ThoughtWorks 2018校园招聘作业题目 -- 出租车_第2张图片

题目分析

这道题本身不难,按照保养规则模拟即可。
但是,对于测试用例二的CAR0001,和CAR0003,根据题目中报废规则,笔者认为属于“报废”。但给出的输出没有包含这两辆车。
如下图所示:

ThoughtWorks 2018校园招聘作业题目 -- 出租车_第3张图片

参考代码

#include 
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;
}

你可能感兴趣的:(模拟)