Week4 CSP-M1 A - cuckoo East adventure

Subject description:

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 to turn the minimum required to get the string

Ideas:

From a number can be transferred to another number x times clockwise or counterclockwise of y, then x ≡ y (mod 26), taking x, y that smaller absolute value, to repeat

to sum up:

Note that the problem can be abstract, abstract to the more simple level, the better

Code:

. 1 #include <cstdio>
 2 #include <the iostream>
 . 3 #include <CString>
 . 4 #include <algorithm>
 . 5  the using  namespace STD;
 . 6  // not necessary to store the letters 
. 7  int Point = 0 , ANS = 0 , now;
 . 8  char T [ 10005 ];
 . 9  int () main
 10  {
 . 11      // absolute value of the minimum of two numbers die 26 with the remaining 
12 is      Scanf ( " % S " , T);
 13 is      int len = strlen (T); 
 14     for(int i=0;i<len;i++)
15     {
16         now=t[i]-'a';
17         int t1=abs(now-point);
18         int t2=abs(26-t1);
19         ans+=min(t1,t2);
20         point=now;
21     }
22     cout<<ans<<endl;
23 }

 

Guess you like

Origin www.cnblogs.com/qingoba/p/12511073.html