#K13711. Minimum Rotations to Unlock the Device
Minimum Rotations to Unlock the Device
Minimum Rotations to Unlock the Device
In this problem, you are given a lock with n wheels, where each wheel displays a digit from 0 to 9. The lock can be unlocked by rotating each wheel to change its current digit to a target digit. Rotating a wheel can be done in either direction. The minimum rotations needed to change a wheel from digit a to digit b is given by the formula:
(\min(|a - b|, 10 - |a - b|)).
Your task is to compute the total number of rotations required for all wheels so that the lock can be unlocked.
Input Example: For n = 3, an initial combination "123" and target combination "321", the minimum rotations required is 4.
Note: The problem requires you to read input from stdin and output the result to stdout.
inputFormat
The input consists of three lines:
- An integer n, representing the number of wheels on the lock.
- A string of n digits representing the initial combination.
- A string of n digits representing the target combination.
outputFormat
Output a single integer which is the minimum number of rotations required to unlock the device.## sample
3
123
321
4