题解 | #手机键盘#

手机键盘

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

全部评论

相关推荐

04-06 11:24
已编辑
太原学院 C++
点赞 评论 收藏
分享
好消息是活的像个人了,周末可以约会吃饭打游戏了坏消息是钱没了,当初来小红书就是为了钱啊哭笑不得😭
犯困嫌疑人:好事儿啊,取消大小周能有更多自己的时间,周末还能约对象玩,这不美滋滋?
投递小红书等公司6个岗位 > 小红书取消大小周
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务