A - Night at the Museum
Problem Link: [Codeforces 731A##link##]
- [tab]
- C++
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { long sum = 0; string s; cin >> s; int strt = 0; for (int i = 0; i < s.size(); i++){ int index = s[i] - 97; if (abs(index - strt) < 13) sum += abs(index - strt); else sum += 26 - abs(index - strt); strt = index; } cout << sum; return 0; }
- Python3
s = int() ref_wheel = int(97) for c in input() : if ord(c) == ref_wheel : continue elif ord(c) < ref_wheel : s += min( ref_wheel - ord(c), (ord(c) - 97) + (123 - ref_wheel) ) ref_wheel = ord(c) continue else : s += min(ord(c) - ref_wheel, (ref_wheel - 97) + (123 - ord(c)) ) ref_wheel = ord(c) continue print(s)
- JAVA
import java.util.Scanner; public class Night_At_the_Museum { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String s = scn.next(); char [] ch = s.toCharArray(); int flag =97; int rotation =0,diff; for(int i=0;i<s.length();i++) { diff = Math.abs(flag-ch[i]); if(diff>13) { diff=26-diff; } rotation += diff; flag =ch[i]; } System.out.println(rotation); } }