题解 | #手机键盘#

手机键盘

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

#include <iostream>
#include<string>
using namespace std;
//abc def ghi
//jkl mno pqrs
//tuv wxyz
const int tab[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};
int main() {
    string s;
    while (cin >> s) {
        int length=s.length();
        int time=0;
        for(int i=0;i<length-1;i++)
        {
            int a=s[i]-'a';
            int b=s[i+1]-'a';
            int c=tab[b]-tab[a];
            int d=b-a;
            //cout<<tab[a]<<" "<<tab[b]<<" "<<c<<" "<<endl;
		  //c==d的判断条件较为巧妙,可以测试两个数据是否在同一键盘上
            if(c==d)
            {time+=2;
            //cout<<"stop"<<endl;
            }
            time+=tab[a];//cout<<"plus"<<tab[a]<<endl;
        }
        time+=tab[s[length-1]-'a'];
        cout<<time<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

机械打工仔:有说的你怀疑一下就行了,直接问也太实诚了
点赞 评论 收藏
分享
07-25 10:17
仰恩大学 营销
bg双非,被挂了
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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