题解 | #手机键盘#
手机键盘
https://www.nowcoder.com/practice/20082c12f1ec43b29cd27c805cd476cd
#include <bits/stdc++.h> using namespace std; int main() { string s; int k[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}; while (cin >> s) { int count = 0 ; for (int i = 0 ; i < s.length() ; i++) { if (i != 0 and s[i] - s[i - 1] == k[s[i] - 'a'] - k[s[i - 1] - 'a']) count = count + 2; count = count + k[s[i] - 'a']; } cout << count << endl; } }
这个条件有点难思考:s[i] - s[i - 1] == k[s[i] - 'a'] - k[s[i - 1] - 'a'])
只想到了s[i] - s[i - 1] == 0
#算法#