题解 | #手机键盘#
手机键盘
https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd
//判断两个相邻字母是否在同一组按键上有其他方式 //若两相邻字母相减之差(0~3)==两相邻字母按键时间之差,则需要加上等待时间 //不过我的方法也很好 #include <iostream> using namespace std; int a[9][5]={ {0,0,0,0,0}, {0,1,2,3,0}, {0,1,2,3,0}, {0,1,2,3,0}, {0,1,2,3,0}, {0,1,2,3,0}, {0,1,2,3,4}, {0,1,2,3,0}, {0,1,2,3,4} }; int b[9][5]={ {0,0,0,0,0}, {0,1,2,3,0}, {0,4,5,6,0}, {0,7,8,9,0}, {0,10,11,12,0}, {0,13,14,15,0}, {0,16,17,18,19}, {0,20,21,22,0}, {0,23,24,25,26} }; int main() { string st; while (cin >> st ) { int sum=0; int len = st.length(); int t1=0,t2=0; for(int i=0;i<=len-1;i++) { t2=t1;//左边的字母 t1=st[i]-'a'+1;//右边的 for(int j=1;j<=8;j++) { for(int k=1;k<=4;k++) { if(t1==b[j][k])sum+=a[j][k]; } } if(t2!=0) { int x,y; for(int j=1;j<=8;j++) { for(int k=1;k<=4;k++) { if(t1==b[j][k])x=j; if(t2==b[j][k])y=j; } } if(x==y) { sum+=2; } } } cout<<sum<<endl; } }