#include <iostream> #include <string> #include <cmath> using namespace std; int main() { string s; cin >> s; int n = s.size(); int total = 0; for (int i = 0; i < n / 2; ++i) {//左半边 int j = n - 1 - i;//对称的右半边 int x = s[i] - 'a'; // 将字符转换为0-25的数值 int y = s[j] - 'a'; int d = abs(x - y); total += min(d, 26 - d); // 累加每对的最少操作次数,正向或逆向 } cout << total << endl; return 0; }
有点难想,字符串的问题,每个要变的最小,转换成数字处理