📌  相关文章
📜  使一个数字可被60整除所需的最少交换次数

📅  最后修改于: 2021-04-23 20:32:01             🧑  作者: Mango

给定整数N ,任务是找到使N可以被60整除所需的最少交换次数。如果不可能,则打印“ -1 ”。

例子:

方法:

  1. 要将数字除以60,必须将其除以2、3和10。因此:
    • 如果数字的数字总和不能被3整除,或者
    • 如果没有可被2整除的数字,或者
    • 如果该数字不包含任何0,

    任何交换都不能使数字被60整除。因此,在这种情况下,需要-1交换

  2. 然后,可以通过以下规则确定所需的最小交换次数:
    • 如果该数字已经可以被60整除,则需要进行0次交换。可以确定最后一位数字(LSB)是否为0,而第二位最后一位数字是否可以被2整除。
    • 如果以下任一情况为真,则需要进行1次交换
      • 如果最后一位数字(LSB)为0并且第二位最后一位数字不能被2整除,或者最后一位数字(LSB)被2分之一并且第二位最后一位数字为0。
      • 如果最后一位数字(LSB)为0,而倒数第二位不能被2整除,则数字中存在多于1个零。
    • 否则需要2次交换

下面是上述方法的实现

CPP
// C++ program to find minimum number
// of swap operations required
#include 
using namespace std;
  
// Function that print minimum number
// of swap operations required
void MinimumSwapOperations(string s)
{
    bool zero_exist = false;
    bool multiple_of_2 = false;
    int sum = 0;
    int index_of_zero;
    bool more_zero = false;
  
    for (int i = 0; i < s.length(); i++) {
        int val = s[i] - '0';
  
        // Condition if more than one
        // zero exist
        if (zero_exist == true)
            more_zero = true;
  
        // Condition if zero_exist
        if (val == 0) {
            zero_exist = true;
            index_of_zero = i;
        }
  
        // Computing total sum of all digits
        sum += val;
    }
  
    // Condition if zero does not exist or
    // the sum is not divisible by 3
    if (zero_exist == false || sum % 3 != 0) {
        cout << "-1"
             << "\n";
        return;
    }
  
    for (int i = 0; i < s.length(); i++) {
        int val = s[i] - '0';
  
        // Condition to find a digit that is
        // multiple of 2 other than one zero
        if (val % 2 == 0 && i != index_of_zero)
            multiple_of_2 = true;
    }
  
    // Condition if multiple of 2
    // do not exist
    if (multiple_of_2 == false) {
        cout << "-1"
             << "\n";
        return;
    }
  
    int last_val = s[s.length() - 1] - '0';
    int second_last_val = s[s.length() - 2]
                          - '0';
  
    // Condition for zero swaps
    // means the number is already
    // is divisible by 60
    if (last_val == 0
        && second_last_val % 2 == 0)
        cout << 0 << "\n";
  
    // Condition for only one swap
    else if ((last_val == 0
              && second_last_val % 2 != 0)
             || (last_val % 2 == 0
                 && second_last_val == 0))
        cout << 1 << "\n";
  
    else if (more_zero == true
             && (last_val == 0
                 && second_last_val % 2 != 0))
        cout << 1 << "\n";
  
    // Otherwise 2 swaps required
    else
        cout << 2 << "\n";
}
  
// Driver Code
int main()
{
    string N = "20";
  
    MinimumSwapOperations(N);
  
    return 0;
}


Java
// Java program to find minimum number
// of swap operations required
class GFG {
      
    // Function that print minimum number
    // of swap operations required
    static void MinimumSwapOperations(String s)
    {
        boolean zero_exist = false;
        boolean multiple_of_2 = false;
        int sum = 0;
        int index_of_zero = 0;
        boolean more_zero = false;
      
        for (int i = 0; i < s.length(); i++) {
            int val = s.charAt(i) - '0';
      
            // Condition if more than one
            // zero exist
            if (zero_exist == true)
                more_zero = true;
      
            // Condition if zero_exist
            if (val == 0) {
                zero_exist = true;
                index_of_zero = i;
            }
      
            // Computing total sum of all digits
            sum += val;
        }
      
        // Condition if zero does not exist or
        // the sum is not divisible by 3
        if (zero_exist == false || sum % 3 != 0) {
            System.out.println("-1");
            return;
        }
      
        for (int i = 0; i < s.length(); i++) {
            int val = s.charAt(i) - '0';
      
            // Condition to find a digit that is
            // multiple of 2 other than one zero
            if (val % 2 == 0 && i != index_of_zero)
                multiple_of_2 = true;
        }
      
        // Condition if multiple of 2
        // do not exist
        if (multiple_of_2 == false) {
            System.out.println("-1");
            return;
        }
      
        int last_val = s.charAt(s.length() - 1)- '0';
        int second_last_val = s.charAt(s.length() - 2)- '0';
      
        // Condition for zero swaps
        // means the number is already
        // is divisible by 60
        if (last_val == 0&& second_last_val % 2 == 0)
            System.out.println(0);
      
        // Condition for only one swap
        else if ((last_val == 0
                  && second_last_val % 2 != 0)
                 || (last_val % 2 == 0
                     && second_last_val == 0))
            System.out.println(1);
      
        else if (more_zero == true
                 && (last_val == 0
                     && second_last_val % 2 != 0))
            System.out.println(1) ;
      
        // Otherwise 2 swaps required
        else
            System.out.println(2) ;
    }
      
    // Driver Code
    public static void main (String[] args)
    {
        String N = "20";
      
        MinimumSwapOperations(N);
      
    }
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 program to find minimum number
# of swap operations required
  
# Function that prminimum number
# of swap operations required
def MinimumSwapOperations(s):
    zero_exist = False
    multiple_of_2 = False
    sum = 0
    index_of_zero = 0
    more_zero = False
  
    for i in range(len(s)):
        val = ord(s[i]) - ord('0')
  
        # Condition if more than one
        # zero exist
        if (zero_exist == True):
            more_zero = True
  
        # Condition if zero_exist
        if (val == 0):
            zero_exist = True
            index_of_zero = i
  
        # Computing total sum of all digits
        sum += val
  
    # Condition if zero does not exist or
    # the sum is not divisible by 3
    if (zero_exist == False or sum % 3 != 0):
        print("-1")
        return
  
    for i in range(len(s)):
        val = ord(s[i]) - ord('0')
  
        # Condition to find a digit that is
        # multiple of 2 other than one zero
        if (val % 2 == 0 and i != index_of_zero):
            multiple_of_2 = True
  
    # Condition if multiple of 2
    # do not exist
    if (multiple_of_2 == False):
        print("-1")
        return
  
    last_val = ord(s[len(s) - 1]) - ord('0')
    second_last_val = ord(s[len(s) - 2])- ord('0')
  
    # Condition for zero swaps
    # means the number is already
    # is divisible by 60
    if (last_val == 0
        and second_last_val % 2 == 0):
        print(0)
  
    # Condition for only one swap
    elif ((last_val == 0
            and second_last_val % 2 != 0)
            or (last_val % 2 == 0
                and second_last_val == 0)):
        print(1)
  
    elif (more_zero == True
            and (last_val == 0
                and second_last_val % 2 != 0)):
        print(1)
  
    # Otherwise 2 swaps required
    else:
        print(2)
  
# Driver Code
if __name__ == '__main__':
    N = "20"
  
    MinimumSwapOperations(N)
  
# This code is contributed by mohit kumar 29


C#
// C# program to find minimum number
// of swap operations required
using System;
  
class GFG {
      
    // Function that print minimum number
    // of swap operations required
    static void MinimumSwapOperations(string s)
    {
        bool zero_exist = false;
        bool multiple_of_2 = false;
        int sum = 0;
        int index_of_zero = 0;
        bool more_zero = false;
      
        for (int i = 0; i < s.Length; i++) {
            int val = s[i] - '0';
      
            // Condition if more than one
            // zero exist
            if (zero_exist == true)
                more_zero = true;
      
            // Condition if zero_exist
            if (val == 0) {
                zero_exist = true;
                index_of_zero = i;
            }
      
            // Computing total sum of all digits
            sum += val;
        }
      
        // Condition if zero does not exist or
        // the sum is not divisible by 3
        if (zero_exist == false || sum % 3 != 0) {
            Console.WriteLine("-1");
            return;
        }
      
        for (int i = 0; i < s.Length; i++) {
            int val = s[i] - '0';
      
            // Condition to find a digit that is
            // multiple of 2 other than one zero
            if (val % 2 == 0 && i != index_of_zero)
                multiple_of_2 = true;
        }
      
        // Condition if multiple of 2
        // do not exist
        if (multiple_of_2 == false) {
            Console.WriteLine("-1");
            return;
        }
      
        int last_val = s[(s.Length - 1)- '0'];
        int second_last_val = s[(s.Length - 2)- '0'];
      
        // Condition for zero swaps
        // means the number is already
        // is divisible by 60
        if (last_val == 0&& second_last_val % 2 == 0)
           Console.WriteLine(0);
      
        // Condition for only one swap
        else if ((last_val == 0
                  && second_last_val % 2 != 0)
                 || (last_val % 2 == 0
                     && second_last_val == 0))
           Console.WriteLine(1);
      
        else if (more_zero == true
                 && (last_val == 0
                     && second_last_val % 2 != 0))
            Console.WriteLine(1) ;
      
        // Otherwise 2 swaps required
        else
            Console.WriteLine(2) ;
    }
      
    // Driver Code
    public static void Main (string[] args)
    {
        string N = "20";
      
        MinimumSwapOperations(N);
      
    }
}
  
// This code is contributed by AnkitRai01


输出:
-1

时间复杂度: O(N)