📜  使一个数能被 4 整除所需的最少位数

📅  最后修改于: 2021-10-26 05:19:43             🧑  作者: Mango

给定一个数字N ,任务是计算要从N 中删除的最小位数,使其可被4整除。

例子:

方法:这个想法是基于可被 4 整除的基本规则,如果一个数的最后两位组成的数能被 4 整除,那么原数也能被4整除。现在,我们的想法是从最后一个检查由两位数字组成的数字是否可以被 4 整除。请按照以下步骤解决问题:

  • 将数字N转换为字符串并将其存储在S 中
  • 使用字符串S的长度初始化变量ans ,以存储所需的最小删除次数。
  • 使用变量i从末尾遍历字符串S
    • 使用变量j在范围[i – 1, 0] 上迭代。
      • 如果S[j]S[i] 组成的数能被 4 整除,则执行以下步骤:
        • 将索引ij之间的位数存储在一个变量中,例如K1 ,它等于(i – j – 1)并将索引i前面的位数存储在一个变量中,例如K2 ,它等于(N – 我 – 1)K1K2的总和表示要删除的位数,使S[j]S[i]成为新号码的最后两位。
        • 如果(K1 + K2)的值小于ans的值,则将ans更新为(K1 + K2)
  • 遍历字符串,如果ans的值仍然不变,则检查是否有任何S[i]可被 4 整除。如果发现为真,则将ans更新为(S 的长度 – 1)
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the minimum number
// of digits required to be removed to
// make a given number divisible by 4
void minimumDeletions(string s)
{
    // Store the size of the string
    int n = s.length();
 
    // Stores the required result
    int ans = n;
 
    // Check for every pair of digits
    // if the number formed by them
    // is divisible by 4 or not
    for (int i = n - 1; i >= 0; i--) {
 
        // Store s[i] in a variable
        int t = s[i] - '0';
 
        // If it is divisible by 2
        if (t % 2 == 0) {
            for (int j = i - 1;
                 j >= 0; j--) {
 
                // Store the number formed
                // by s[j] and s[i]
                int num = (s[j] - '0')
                              * 10
                          + t;
 
                // Check if it is
                // divisible by 4
                if (num % 4 == 0) {
 
                    // Store the number of digits
                    // required to be deleted
                    int k1 = i - j - 1;
                    int k2 = n - i - 1;
 
                    // Update ans
                    ans = min(ans,
                              k1 + k2);
                }
            }
        }
    }
 
    // If value of ans is unchanged, then
    // check if any s[i] is divisible by 4
    if (ans == n) {
 
        for (int i = 0; i < n; i++) {
 
            int num = s[i] - '0';
 
            // If true, update ans to n - 1
            if (num % 4 == 0) {
                ans = n - 1;
            }
        }
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    string str = "12367";
    minimumDeletions(str);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
  // Function to count the minimum number
  // of digits required to be removed to
  // make a given number divisible by 4
  static void minimumDeletions(String s)
  {
 
    // Store the size of the string
    int n = s.length();
 
    // Stores the required result
    int ans = n;
 
    // Check for every pair of digits
    // if the number formed by them
    // is divisible by 4 or not
    for (int i = n - 1; i >= 0; i--) {
 
      // Store s[i] in a variable
      int t = s.charAt(i) - '0';
 
      // If it is divisible by 2
      if (t % 2 == 0) {
        for (int j = i - 1; j >= 0; j--) {
 
          // Store the number formed
          // by s[j] and s[i]
          int num = (s.charAt(j) - '0') * 10 + t;
 
          // Check if it is
          // divisible by 4
          if (num % 4 == 0) {
 
            // Store the number of digits
            // required to be deleted
            int k1 = i - j - 1;
            int k2 = n - i - 1;
 
            // Update ans
            ans = Math.min(ans, k1 + k2);
          }
        }
      }
    }
 
    // If value of ans is unchanged, then
    // check if any s[i] is divisible by 4
    if (ans == n) {
 
      for (int i = 0; i < n; i++) {
 
        int num = s.charAt(i) - '0';
 
        // If true, update ans to n - 1
        if (num % 4 == 0) {
          ans = n - 1;
        }
      }
    }
 
    // Print the result
    System.out.println(ans);
  }
 
  // Driver Code
  static public void main(String[] args)
  {
    String str = "12367";
    minimumDeletions(str);
  }
}
 
// This code is contributed by ukasp.


Python3
# Python3 program for the above approach
 
# Function to count the minimum number
# of digits required to be removed to
# make a given number divisible by 4
def minimumDeletions(s):
   
    # Store the size of the string
    n = len(s)
 
    # Stores the required result
    ans = n
 
    # Check for every pair of digits
    # if the number formed by them
    # is divisible by 4 or not
    for i in range(n - 1, -1, -1):
 
        # Store s[i] in a variable
        t = ord(s[i]) - ord('0')
 
        # If it is divisible by 2
        if (t % 2 == 0):
            for j in range(i - 1, -1, -1):
               
                # Store the number formed
                # by s[j] and s[i]
                num = (ord(s[j]) - ord('0'))* 10 + t
                 
                # Check if it is
                # divisible by 4
                if (num % 4 == 0):
                   
                    # Store the number of digits
                    # required to be deleted
                    k1 = i - j - 1
                    k2 = n - i - 1
 
                    # Update ans
                    ans = min(ans, k1 + k2)
 
    # If value of ans is unchanged, then
    # check if any s[i] is divisible by 4
    if (ans == n):
        for i in range(n):
            num = ord(s[i]) - ord('0')
 
            # If true, update ans to n - 1
            if (num % 4 == 0):
                ans = n - 1
 
    # Prthe result
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    str = "12367"
    minimumDeletions(str)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to count the minimum number
// of digits required to be removed to
// make a given number divisible by 4
static void minimumDeletions(string s)
{
   
    // Store the size of the string
    int n = s.Length;
 
    // Stores the required result
    int ans = n;
 
    // Check for every pair of digits
    // if the number formed by them
    // is divisible by 4 or not
    for (int i = n - 1; i >= 0; i--) {
 
        // Store s[i] in a variable
        int t = s[i] - '0';
 
        // If it is divisible by 2
        if (t % 2 == 0) {
            for (int j = i - 1;
                 j >= 0; j--) {
 
                // Store the number formed
                // by s[j] and s[i]
                int num = (s[j] - '0')
                              * 10
                          + t;
 
                // Check if it is
                // divisible by 4
                if (num % 4 == 0) {
 
                    // Store the number of digits
                    // required to be deleted
                    int k1 = i - j - 1;
                    int k2 = n - i - 1;
 
                    // Update ans
                    ans = Math.Min(ans,
                              k1 + k2);
                }
            }
        }
    }
 
    // If value of ans is unchanged, then
    // check if any s[i] is divisible by 4
    if (ans == n) {
 
        for (int i = 0; i < n; i++) {
 
            int num = s[i] - '0';
 
            // If true, update ans to n - 1
            if (num % 4 == 0) {
                ans = n - 1;
            }
        }
    }
 
    // Print the result
    Console.WriteLine(ans);
}
 
// Driver Code
static public void Main()
{
    string str = "12367";
    minimumDeletions(str);
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
1

时间复杂度: O((log 10 N) 2 )
辅助空间: O (1)