题意
咕咕东是个贪玩的孩子,有一天,他从上古遗迹中得到了一个神奇的圆环。这个圆环由字母表组成首尾相接的环,环上有一个指针,最初指向字母a。咕咕东每次可以顺时针或者逆时针旋转一格。例如,a顺时针旋转到z,逆时针旋转到b。咕咕东手里有一个字符串,但是他太笨了,所以他来请求你的帮助,问最少需要转多少次。
思路
求字符字母之间的最短距离
算出顺时针的距离d之后
用26减去这个距离d就是逆时针的距离
总结
第一次模拟有点紧张,写了个沙雕东西,现在改了
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin>>s;
int ans = 0;
string a = "a";
string test = a + s;
char b = test[0];
for (int i = 1; i < test.size(); i++)
{
int length1;
if (test[i] > b)
length1 = test[i] - b;
else
length1 = b - test[i];
int length2 = 26 - length1;
if (length1 < length2)
ans = ans + length1;
else
ans = ans + length2;
b = test[i];
}
cout<<ans<<endl;
return 0;
}