题解 | #手机键盘#

手机键盘

https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd

#include <stdio.h>
#include <string.h>

int map(char x){
    if(x=='a' || x=='d' || x=='g' || x=='j' || x=='m' || x=='p' || x=='t' || x=='w'){
        return 1;
    }
    else if(x=='b' || x=='e' || x=='h' || x=='k' || x=='n' || x=='q' || x=='u' || x=='x'){
        return 2;
    }
    else if(x=='c' || x=='f' || x=='i' || x=='l' || x=='o' || x=='r' || x=='v' || x=='y'){
        return 3;
    }
    else if (x=='s' || x=='z'){
        return 4;
    }
    return 0;
}

int group(char x){
    if (x=='a' || x=='b' || x=='c'){
        return 1;
    }
    else if (x=='d' || x=='e' || x=='f'){
        return 2;
    }
    else if (x=='g' || x=='h' || x=='i'){
        return 3;
    }
    else if (x=='j' || x=='k' || x=='l'){
        return 4;
    }
    else if (x=='m' || x=='n' || x=='o'){
        return 5;
    }
    else if (x=='p' || x=='q' || x=='r' || x=='s'){
        return 6;
    }
    else if (x=='t' || x=='u' || x=='v'){
        return 7;
    }
    else if (x=='w' || x=='x' || x=='y' || x=='z'){
        return 8;
    }
    return 0;
}

int main(){
    char str[101] = {'\0'};
    while (scanf("%s", str) != EOF){
        int len = strlen(str);
        int time = 0;
        for (int i = 0; i < len; ++i){
            if((group(str[i]) == group(str[i+1])) && str[i+1] != '\0'){
                time += 2;
            }
            time += map(str[i]);
        }
        printf("%d\n", time);
    }
    return 0;
}

全部评论

相关推荐

孙艹肘:校招不给三方直接让实习我都去了,,主打一个在学校呆着也是闲着,不如出来实习一下
点赞 评论 收藏
分享
10-22 12:03
山东大学 Java
程序员小白条:26届一般都得有实习,项目可以随便写的,如果不是开源社区的项目,随便包装,技术栈也是一样,所以本质应该找学历厂,多投投央国企和银行,技术要求稍微低一点的,或者国企控股那种,纯互联网一般都得要干活
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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