题解 | #手机键盘#
手机键盘
https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd
#include <iostream>
#include <cstring>
using namespace std;
int a[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 b[26] = {1, 1, 1, 2 , 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8};
int main() {
string str;
while(cin >> str){
int time = 0;
int last = 0;
for(int i = 0; i < str.size(); i++){
int x = str[i] - 'a';
int lx = str[i-1] - 'a';
time += a[x];
if(i > 0){
time += b[x] != b[lx] ? 0 : 2;
}
}
cout << time <<endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")

查看6道真题和解析