CSP-M1 A title

Meaning of the questions:
Cuckoo East is a playful child, one day, he got a magic ring from the ancient ruins. The ring formed by the annular butt letters. Initially points to put letters a, the pointer can be rotated clockwise or counterclockwise rotation, such as a clockwise rotation to B, or counterclockwise rotation to z. . Cuckoo East hands of a string, but he is dumb, so he was coming to you for help, asked how many times requires a minimum of turn. Here Insert Picture Description
input:
enter a string.
output:
output required minimum number of revolutions.
INPUT Sample:
hezt
Sample Output:
31 is
the idea:
Enter a memory array of letters, each letter for the array by converting the location marked letters. Length of the input string is to be determined is input, determination is circulated, if the character 13 is smaller than or equal distance from the starting point of the initial array, a distance clockwise description small, the count start point and the character arrays together where bit of difference, mark this as an array of characters after the starting point. If the distance is greater than 13, then that counterclockwise rotation distance is small, the count plus the number of steps to go counter-clockwise, the starting point of an array of characters updates to this end. The number of steps resulting output after the end of the last cycle.
Previous attempts without conversion map, only the direct memory array, the entire array is determined through left shift operation, but the three points oj make life difficult. This representation can be changed to success.
Code:

#include<iostream>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
map<char,int> am;
int main()
{
 am['a']=0;
 am['b']=1;
 am['c']=2;
 am['d']=3;
 am['e']=4;
 am['f']=5;
 am['g']=6;
 am['h']=7;
 am['i']=8;
 am['j']=9;
  am['k']=10;
 am['l']=11;
 am['m']=12;
 am['n']=13;
 am['o']=14;
 am['p']=15;
 am['q']=16;
 am['r']=17;
 am['s']=18;
 am['t']=19;
 am['u']=20;
 am['v']=21;
 am['w']=22;
 am['x']=23;
 am['y']=24;
 am['z']=25;
  string a;
 cin>>a;
 
 int count=0;
 int op=0;
 int oi=0;
 //for(int i=0;i<length;i++)
 for(int i=0;i<a.length();i++)
 {
  op=am[a[i]];
  if(abs(op-oi)<=13)
  {
   count=count+abs(op-oi);
   oi=op;
  }
  else if(abs(op-oi)>13)
  {
   count=count+26-abs(op-oi);
   oi=op;
  }
   }
 cout<<count;
 return 0;
}
Published 19 original articles · won praise 0 · Views 214

Guess you like

Origin blog.csdn.net/weixin_45117273/article/details/104982259