Competition1: Cuckoo East adventure

Title Contents
cuckoo East is a playful child, one day, he got a magic ring from the ancient ruins. This ring from an alphabet consisting of end to end rings, a ring pointer initially points to the letter a. Cuckoo East each can rotate clockwise or counter-clockwise one space. For example, a clockwise rotation through z, counterclockwise rotation to b. Cuckoo East hands of a string, but he is dumb, so he came to ask your help, ask how many times requires a minimum of turn.
Here Insert Picture Description

Input format
Enter only one line, it is a string.

Output format
output count to a minimum turn.

Input Example

zeus

Output Example

18

Problem-solving ideas
This question can actually be a lot of ideas, but not difficult.
I choose to direct each letter from other pretreatment letters, are combined into a second-order array, use the time to extract out like.

#include<iostream>
#include<string>
#include<cstring>

using namespace std;

string input;

//A B C D E F G
//H I J K L M N=13[12]
//O P Q   R S T
//U V W   X Y Z
int dis[26][26];

int CtoI(char letter)
{
	int out;
	if(letter>='A'&&letter<='Z')
	{
		out=letter-'A';
		return out;
	}
	if(letter>='a'&&letter<='z')
	{
		out=letter-'a';
		return out;
	}
}

int main()
{
    memset(dis,0,sizeof(dis));
	
	int t;
	for(int l=0;l<=25;l++)
	{
		t=0;
		for(int i=l;i<=25;i++)
		{
			dis[l][i]=t;
			dis[i][l]=t;
			if(i<13+l)
			{
				t++;
			}
			else
			{
				t--;
			}
		}
	}

	cin>>input;
	
	int ans=0;
	
	int last=0;
	int now=0;
	for(int i=0;i<input.length();i++)
	{
		now=CtoI(input[i]);
		ans+=dis[last][now];
		last=now;
	}
	
	cout<<ans;
	
	
	return 0;
}
Published 21 original articles · won praise 3 · Views 413

Guess you like

Origin blog.csdn.net/qq_44506233/article/details/104981905