题解 | #手机键盘#
手机键盘
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")
美的集团公司福利 798人发布