题解 | #手机键盘#

手机键盘

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

//手机键盘 清华 2024/1/4
#include <iostream>
using namespace std;

int main() {
    string str;
    int len, count;
    int hit_wait[26] = {//数组记录每个字母对应的敲击时间
        1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3,
        1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4
    };
    while (cin >> str) {
        count = hit_wait[str[0]-'a'];
        len = str.length();
        for (int i = 1; i < len; i++) {
            count += hit_wait[str[i] - 'a'];
            if((str[i]-str[i-1])==(hit_wait[str[i]-'a']-hit_wait[str[i-1]-'a'])){
                //此处注意判断条件的书写
                count+=2;
            }
        }
        cout << count << endl;
    }
  return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-30 18:19
点赞 评论 收藏
分享
牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
兄弟们,实习都是在接各种api,该怎么包装简历
仁者伍敌:感觉我自己做小项目也是各种api啊,我要怎么包装简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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