题解 | 手机键盘

手机键盘

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

#include <iostream>
using namespace std;
//构建判定分组的辅助数组和判定次数的辅助数组
int main() {
    string str;
    while (cin >> str) {
        int len = str.length();
        int cnt = 0;
        int pd[26];
        int cost[26];
        for (int i = 0; i < 26; i++) {
            if (i <= 14) {
                pd[i] = i / 3;
                cost[i] = i % 3 + 1;
            } else if (i > 14 and i <= 18) {
                pd[i] = 5;    //pqrs属于第5组
                cost[i] = i - 14;
            } else if (i > 18 and i <= 21) {
                pd[i] = 6;
                cost[i] = i - 18;
            }
            else
            {
                pd[i] = 7;
                cost[i] = i - 21;
            }

        }

        for (int i = 0; i < len - 1; i++) {
            int num1 = str[i] - 'a';
            int num2 = str[i + 1] - 'a';
            if (pd[num1] == pd[num2]) cnt += 2;
            cnt += cost[num1];
        }
        cnt += cost[str[len-1]-'a'];
        cout << cnt << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

暴杀流调参工作者:春招又试了一些岗位,现在投递很有意思,不仅要精心准备简历,投递官网还得把自己写的东西一条一条复制上去,阿里更是各个bu都有自己的官网,重复操作无数次,投完简历卡完学历了,又该写性格测评、能力测评,写完了又要写专业笔试,最近还有些公司搞了AI辅助编程笔试,有些还有AI面试,对着机器人话也听不明白录屏硬说,终于到了人工面试又要一二三四面,小组成员面主管面部门主管面hr面,次次都没出错机会,稍有不慎就是挂。 卡学历卡项目卡论文卡实习什么都卡,没有不卡的😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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