题目连接:http://www.rqnoj.cn/Problem_224.html
解题思路:将对应按键存成常量数组。
#include <stdio.h> #include <string.h> const int sign[] = {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}; const int N = 205; int main() { char str[N]; while (gets(str)) { int len = strlen(str), cnt = 0; for (int i = 0; i < len; i++) if (str[i] == ' ') cnt++; else cnt += sign[str[i] - 'a']; printf("%d\n", cnt); } return 0; }