您将获得一个锁,该锁由n个不同的圆环组成,每个圆环上依次印有0-9位数字。最初,所有n环一起显示一个n位整数,但是只有特定的代码可以打开锁。您可以沿任一方向将每个环旋转任意次数。您必须找到在锁环上完成的最小旋转次数才能打开锁。
例子:
Input : Input = 2345, Unlock code = 5432
Output : Rotations required = 8
Explanation : 1st ring is rotated thrice as 2->3->4->5
2nd ring is rotated once as 3->4
3rd ring is rotated once as 4->3
4th ring is rotated thrice as 5->4->3->2
Input : Input = 1919, Unlock code = 0000
Output : Rotations required = 4
Explanation : 1st ring is rotated once as 1->0
2nd ring is rotated once as 9->0
3rd ring is rotated once as 1->0
4th ring is rotated once as 9->0
对于单个环,我们可以按以下两个方向中的任意一个将其向前或向后旋转:
- 0-> 1-> 2….-> 9-> 0
- 9-> 8->….0-> 9
但是我们担心所需的最小旋转数,因此我们应该选择min(abs(ab),10-abs(ab)),因为ab表示正向旋转数,而10-abs(ab)表示向后旋转数从a旋转到b的环。此外,我们必须找到每个数字的每个环的最小数量。因此,从最右边的数字开始,我们可以轻松地找到每个环所需的最小旋转数,最后到最左边的数字结束。
C++
// CPP program for min rotation to unlock
#include
using namespace std;
// function for min rotation
int minRotation(int input, int unlock_code)
{
int rotation = 0;
int input_digit, code_digit;
// iterate till input and unlock code become 0
while (input || unlock_code) {
// input and unlock last digit as reminder
input_digit = input % 10;
code_digit = unlock_code % 10;
// find min rotation
rotation += min(abs(input_digit - code_digit),
10 - abs(input_digit - code_digit));
// update code and input
input /= 10;
unlock_code /= 10;
}
return rotation;
}
// driver code
int main()
{
int input = 28756;
int unlock_code = 98234;
cout << "Minimum Rotation = "
<< minRotation(input, unlock_code);
return 0;
}
Java
// Java program for min rotation to unlock
class GFG
{
// function for min rotation
static int minRotation(int input, int unlock_code)
{
int rotation = 0;
int input_digit, code_digit;
// iterate till input and unlock code become 0
while (input>0 || unlock_code>0) {
// input and unlock last digit as reminder
input_digit = input % 10;
code_digit = unlock_code % 10;
// find min rotation
rotation += Math.min(Math.abs(input_digit
- code_digit), 10 - Math.abs(
input_digit - code_digit));
// update code and input
input /= 10;
unlock_code /= 10;
}
return rotation;
}
// driver code
public static void main (String[] args) {
int input = 28756;
int unlock_code = 98234;
System.out.println("Minimum Rotation = "+
minRotation(input, unlock_code));
}
}
/* This code is contributed by Mr. Somesh Awasthi */
Python3
# Python3 program for min rotation to unlock
# function for min rotation
def minRotation(input, unlock_code):
rotation = 0;
# iterate till input and unlock
# code become 0
while (input > 0 or unlock_code > 0):
# input and unlock last digit
# as reminder
input_digit = input % 10;
code_digit = unlock_code % 10;
# find min rotation
rotation += min(abs(input_digit - code_digit),
10 - abs(input_digit - code_digit));
# update code and input
input = int(input / 10);
unlock_code = int(unlock_code / 10);
return rotation;
# Driver Code
input = 28756;
unlock_code = 98234;
print("Minimum Rotation =",
minRotation(input, unlock_code));
# This code is contributed by mits
C#
// C# program for min rotation to unlock
using System;
class GFG {
// function for min rotation
static int minRotation(int input,
int unlock_code)
{
int rotation = 0;
int input_digit, code_digit;
// iterate till input and
// unlock code become 0
while (input > 0 ||
unlock_code > 0)
{
// input and unlock last
// digit as reminder
input_digit = input % 10;
code_digit = unlock_code % 10;
// find min rotation
rotation += Math.Min(Math.Abs(input_digit -
code_digit), 10 - Math.Abs(
input_digit - code_digit));
// update code and input
input /= 10;
unlock_code /= 10;
}
return rotation;
}
// Driver Code
public static void Main ()
{
int input = 28756;
int unlock_code = 98234;
Console.Write("Minimum Rotation = "+
minRotation(input, unlock_code));
}
}
// This code is contributed by Nitin Mittal
PHP
输出:
Minimum Rotation = 12