题解 | #手机键盘#
手机键盘
https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd
#include<iostream> using namespace std; string str[] = {"","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; int find_key(char c) { int ans = -1; for(int i = 1;i < 9;i++) { if(str[i].find(c,0) != -1) ans = i; } return ans; } int find_pos(char c) { int ans = -1; for(int i = 1;i < 9;i++) { int pos = str[i].find(c,0); if(pos != -1 )ans = pos + 1; } return ans; } int main(void) { string s; while(cin >> s) { int time = 0; int preKey = -1; for(int i = 0;i < s.size();i++) { int key = find_key(s[i]); int pos = find_pos(s[i]); if(preKey != key){ preKey = key; time += pos; }else{ time += 2; time += pos; } } cout << time << endl; } return 0; }