题解 | #配置文件恢复#

配置文件恢复

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

题意先匹配第一关键字再匹配第二关键字容易误导,一起匹配就好了。
先判断第二关键字是否为空
#include <stdio.h>
#include <string.h>
#define SIZE 20

const char *result[7] = {
    "unknown command","reset what","board fault","where to add",
    "no board at all","impossible","install first"
};

int match(char *arr);

int main(void)
{
    char arr[SIZE+2];
    while(fgets(arr,SIZE+1,stdin)){
        if(arr[strlen(arr) - 1] == '\n') arr[strlen(arr) - 1] = '\0';
        printf("%s\n",result[match(arr)]);
    }
    return 0;
}

int match(char *arr)
{
    char *first,*second,*pt;
    int ans = 0;
    
    pt = arr;
    while(*pt != ' ' && *pt != '\0') pt++;
    if(*pt = ' ') *pt++ = '\0';
    first = arr;
    second = pt;
    
    if(*second == '\0'){
        if(strstr("reset",first)) ans = 1;
    }else{
        if(strstr("reset",first) && strstr("board",second)){
            if(strstr("reboot",first) && strstr("backplane",second)) ans = 0;
            else ans = 2;
        }else if(strstr("board",first) && strstr("add",second)){
            if(strstr("backplane",first) && strstr("abort",second)) ans = 0;
            else ans = 3;
        }else if(strstr("board",first) && strstr("delete",second)){
            ans = 4;
        }else if(strstr("reboot",first) && strstr("backplane",second)){
            ans = 5;
        }else if(strstr("backplane",first) && strstr("abort",second)){
            ans = 6;
        }
    }
    return ans;
}



全部评论

相关推荐

点赞 评论 收藏
分享
08-23 20:17
四川大学 C++
xxxzsfa:技术栈可以参考我这个给写,把知识点写出来起码能让面试官知道你是知道这个点的,不会的知识点可以不写,简历上写了但又答不上来就很尴尬了,可以给技术栈中的知识点和项目中的难点亮点加粗,学历不错,肯定会有大厂面的,刚开始可以先投中小厂练手,拿个offer保底再去面大厂,秋招可以正式和实习一起找,找工作是个需要长期坚持的过程,加油查看图片
点赞 评论 收藏
分享
10-11 14:44
济南大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务