📜  解锁圆形锁的最小旋转

📅  最后修改于: 2021-10-26 06:37:12             🧑  作者: Mango

给你一个锁,它由 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


Javascript


输出:

Minimum Rotation = 12

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程