题解 | 手机键盘

手机键盘

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

#include <iostream>
#include <string>
#include <map>
using namespace std;


int main() {
    map<char, int> map_value = {
     {'a',1},{'b',2},{'c',3},{'d',1},{'e',2},{'f',3},{'g',1},{'h',2},{'i',3},{'j',1},{'k',2},{'l',3},{'m',1},{'n',2},{'o',3},{'p',1},{'q',2},{'r',3},{'s',4},{'t',1},{'u',2},{'v',3},{'w',1},{'x',2},{'y',3},{'z',4}
    };
    string str;
    map<char, int> map_button = {
        {'a',1},{'b',1},{'c',1},{'d',2},{'e',2},{'f',2},{'g',3},{'h',3},{'i',3},{'j',4},{'k',4},{'l',4},{'m',5},{'n',5},{'o',5},{'p',6},{'q',6},{'r',6},{'s',6},{'t',7},{'u',7},{'v',7},{'w',8},{'x',8},{'y',8},{'z',8}
    };
    while (cin >> str)
    {
        int times = 0;
        int long_of_str = str.size();
        for (int i = 0; i < long_of_str; i++)
        {
            times += map_value[str[i]];
            if (i < long_of_str)
            {
                if (map_button[str[i]] == map_button[str[i + 1]])times += 2;
            }
        }
        cout << times << endl;
    }

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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