题解 | #手机键盘#

手机键盘

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

全部评论

相关推荐

听说改名字就能收到offer哈:Radis写错了兄弟
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务