题解 | #手机键盘#
手机键盘
https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd
#include <iostream>
#include <tuple>
using namespace std;
// keytable 当要二十六个字母映射为按键次数时,直接使用数组。
int keytable[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 str;
while(cin>>str)
{
int times = 0 ;
for(int i = 0; i < str.size() ; ++ i)
{
times += keytable[str[i] - 'a'] ;
if(i != 0 && str[i] - str[i-1] == keytable[str[i] - 'a']- keytable[str[i-1] - 'a'])
{// 有的键盘是4个字母, 有的键盘是3个字母所以对3取模判断两个字母是否在同一个按键 行不通; 换个思路如果是在同一个按键,那么字母本身之间的距离等于它们按键之差。
times+= 2;
}
}
cout<<times<<endl;
}
}
// 64 位输出请用 printf("%lld")

