题解 | #手机键盘# 手机键盘 九宫格
手机键盘
https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
int main() {
map<int, char> input_time
= { // 现在假设每按一次需要花费一个时间段,
{'a', 1}, {'b', 2}, {'c', 3},
{'d', 1}, {'e', 2}, {'f', 3},
{'g', 1}, {'h', 2}, {'i', 3},
{'j', 1}, {'k', 2}, {'l', 3},
{'m', 1}, {'n', 2}, {'o', 3},
{'p', 1}, {'q', 2}, {'r', 3}, {'s', 4},
{'t', 1}, {'u', 2}, {'v', 3},
{'w', 1}, {'x', 2}, {'y', 3}, {'z', 4},
};
map<char, int> keyMap
= { // 字母对应的按键 需要用输入的字母找键盘的按键来计算
{'a', 2}, {'b', 2}, {'c', 2},
{'d', 3}, {'e', 3}, {'f', 3},
{'g', 4}, {'h', 4}, {'i', 4},
{'j', 5}, {'k', 5}, {'l', 5},
{'m', 6}, {'n', 6}, {'o', 6},
{'p', 7}, {'q', 7}, {'r', 7}, {'s', 7},
{'t', 8}, {'u', 8}, {'v', 8},
{'w', 9}, {'x', 9}, {'y', 9}, {'z', 9},
};
char str[100];
string str2;
while (EOF != scanf("%s", str)) {
str2 = str;
int len = str2.length();
int last_key = 1, total_time = 0,
wait_time = 2; // 上一次输入的按键 默认为不带字母的1
for (int i = 0; i < len; ++i) {
if (last_key == keyMap[str[i]]) {
total_time +=
wait_time; // 总时间每次+2 等待时间需要花费两个时间段。
}
total_time += input_time[str[i]];
last_key = keyMap[str[i]];
}
printf("%d\n", total_time);
}
}
#机试#
海康威视公司福利 1182人发布
查看9道真题和解析