题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    string inp;
    vector<vector<string>> coms(5);
    coms[0] = {"reset", "board", "board fault"};
    coms[1] = {"board", "add", "where to add"};
    coms[2] = {"board", "delete", "no board at all"};
    coms[3] = {"reboot", "backplane", "impossible"};
    coms[4] = {"backplane", "abort", "install first"};

    while (getline(cin, inp)) { // 注意 while 处理多个 case
        int spacePos = inp.find(' ');
        if (spacePos == string::npos) {
            string command = "reset";
            if (command.find(inp) == 0) {
                cout << "reset what" << endl;
            }
            else {
                cout << "unknown command" << endl;
            }
        }
        else {
            string inp1 = inp.substr(0, spacePos);
            string inp2 = inp.substr(spacePos + 1, inp.size() - spacePos - 1);
            string result;
            int matchCnt = 0;
            for (int i = 0; i < 5; ++i) {
                if (coms[i][0].find(inp1) == 0 && coms[i][1].find(inp2) == 0) {                
                    result = coms[i][2];
                    ++matchCnt;                 
                }
            }
            if (matchCnt == 1) cout << result << endl;
            else cout << "unknown command" << endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

10-24 11:10
山西大学 Java
若梦难了:哥们,面试挂是很正常的。我大中厂终面挂,加起来快10次了,继续努力吧。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务