📌  相关文章
📜  要删除的位数以使数字可以被 25 整除

📅  最后修改于: 2022-05-13 01:56:10.555000             🧑  作者: Mango

要删除的位数以使数字可以被 25 整除

给定一个数字N ,任务是找到需要从该数字中删除的最小位数,以便该数字可以被 25 整除。

方法:一个数可以被 25 整除,如果它的最后两位数字是“00”或者由它的最后两位数字组成的数字可以被 25 整除,如检查一个大数是否能被 25 整除中解释。现在,在这个问题中,检查 N 中所有可能的对的这个条件,并找到需要删除的最小位数。如果找到任何一对元素满足上述条件,则可以形成一个以这两个元素作为最后一位的数,那么它将是 25 的倍数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the
int minDigits(int n)
{
    vector str;
    // Convert number into string
    int i = 0;
    while (n != 0) {
        int rem = n % 10;
 
        // convert int into char
        // by adding '0'
        char ch = (rem + '0');
        str.push_back(ch);
        n /= 10;
    }
 
    // Reverse string
    reverse(str.begin(), str.end());
 
    int ans = INT_MAX;
    int N = str.size();
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
 
            // Number formed by
            // last two digits
            int num = (str[i] - '0')
                          * 10
                      + (str[j] - '0');
 
            if (num % 25 == 0) {
 
                // Count of unwanted digits
                // between i and j
                int a = j - i - 1;
 
                // Count of unwanted
                // digits after j
                int b = N - (j + 1);
                ans = min(ans, a + b);
            }
        }
    }
 
    return ans;
}
 
// Driver Code
int main()
{
 
    int n = 71345;
    int ans = minDigits(n);
    if (ans == INT_MAX) {
        cout << -1;
    }
    else {
        cout << ans;
    }
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
  // Function to find the
  static int minDigits(int n)
  {
    Vector str = new Vector();
 
    // Convert number into String
    int i = 0;
    while (n != 0) {
      int rem = n % 10;
 
      // convert int into char
      // by adding '0'
      char ch = (char) (rem + '0');
      str.add(ch);
      n /= 10;
    }
 
    // Reverse String
    Collections.reverse(str);
 
    int ans = Integer.MAX_VALUE;
    int N = str.size();
    for (i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
 
        // Number formed by
        // last two digits
        int num = (str.get(i) - '0')
          * 10
          + (str.get(j) - '0');
 
        if (num % 25 == 0) {
 
          // Count of unwanted digits
          // between i and j
          int a = j - i - 1;
 
          // Count of unwanted
          // digits after j
          int b = N - (j + 1);
          ans = Math.min(ans, a + b);
        }
      }
    }
 
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    int n = 71345;
    int ans = minDigits(n);
    if (ans == Integer.MAX_VALUE) {
      System.out.print(-1);
    }
    else {
      System.out.print(ans);
    }
  }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python code for the above approach
 
# Function to find the
def minDigits(n):
    str = []
 
    # Convert number into string
    i = 0
    while (n != 0):
        rem = n % 10
 
        # convert int into char
        # by adding '0'
        ch = chr(rem + ord('0'))
        str.append(ch)
        n = (n // 10)
 
    # Reverse string
    str.reverse()
 
    ans = 10 ** 9
    N = len(str)
    for i in range(N):
        for j in range(i + 1, N):
 
            # Number formed by
            # last two digits
            num = (ord(str[i]) - ord('0')) * 10 + (ord(str[j]) - ord('0'))
 
            if (num % 25 == 0):
                # Count of unwanted digits
                # between i and j
                a = j - i - 1
 
                # Count of unwanted
                # digits after j
                b = N - (j + 1)
                ans = min(ans, a + b)
 
    return ans
 
# Driver Code
n = 71345
ans = minDigits(n)
if (ans == 10 ** 9):
    print(-1)
else:
    print(ans)
 
# This code is contributed by Saurabh Jaiswal;


C#
// C# program for the above approach
using System;
using System.Collections;
 
class GFG
{
// Function to find the
static int minDigits(int n)
{
    ArrayList str = new ArrayList();
   
    // Convert number into string
    while (n != 0) {
        int rem = n % 10;
 
        // convert int into char
        // by adding '0'
        char ch = (char)(rem + '0');
        str.Add(ch);
        n /= 10;
    }
 
    // Reverse string
    str.Reverse();
    int ans = Int32.MaxValue;
    int N = str.Count;
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
 
            // Number formed by
            // last two digits
            int num = ((char)str[i] - '0')
                          * 10
                      + ((char)str[j] - '0');
 
            if (num % 25 == 0) {
 
                // Count of unwanted digits
                // between i and j
                int a = j - i - 1;
 
                // Count of unwanted
                // digits after j
                int b = N - (j + 1);
                ans = Math.Min(ans, a + b);
            }
        }
    }
 
    return ans;
}
 
// Driver Code
public static void Main()
{
 
    int n = 71345;
    int ans = minDigits(n);
    if (ans == Int32.MaxValue) {
        Console.Write(-1);
    }
    else {
        Console.Write(ans);
    }
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出:
3

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