📌  相关文章
📜  除去数字后,将数字减小到最小4的倍数

📅  最后修改于: 2021-05-19 19:47:34             🧑  作者: Mango

给定一个整数N ,任务是在除去一些数字(可能没有数字)之后,将数字减小为最小的正整数X ,以使X可以被4整除。如果不能将其减小到-1,则打印-1

例子:

方法:由于必须将结果数量最小化。因此,请检查数字中是否有任何等于“ 4”“ 8”的数字,因为这些数字是按升序可被4整除的数字。如果没有这样的数字,则检查长度为2的数字的所有子序列是否为4的倍数。如果仍然没有4的倍数,则该数字是不可能的,因为任何2位数以上的数字都是4的倍数,那么肯定会有一个被4整除的子序列,而数字要少3

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
const int TEN = 10;
  
// Function to return the minimum number
// that can be formed after removing
// the digits which is a multiple of 4
int minNum(string str, int len)
{
    int res = INT_MAX;
  
    // For every digit of the number
    for (int i = 0; i < len; i++) {
  
        // Check if the current digit
        // is divisible by 4
        if (str[i] == '4' || str[i] == '8') {
            res = min(res, str[i] - '0');
        }
    }
  
    for (int i = 0; i < len - 1; i++) {
        for (int j = i + 1; j < len; j++) {
            int num = (str[i] - '0') * TEN
                      + (str[j] - '0');
  
            // If any subsequence of two
            // digits is divisible by 4
            if (num % 4 == 0) {
                res = min(res, num);
            }
        }
    }
  
    return ((res == INT_MAX) ? -1 : res);
}
  
// Driver code
int main()
{
    string str = "17";
    int len = str.length();
  
    cout << minNum(str, len);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
static int TEN = 10;
  
// Function to return the minimum number
// that can be formed after removing
// the digits which is a multiple of 4
static int minNum(char[] str, int len)
{
    int res = Integer.MAX_VALUE;
  
    // For every digit of the number
    for (int i = 0; i < len; i++)
    {
  
        // Check if the current digit
        // is divisible by 4
        if (str[i] == '4' || str[i] == '8')
        {
            res = Math.min(res, str[i] - '0');
        }
    }
  
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = i + 1; j < len; j++) 
        {
            int num = (str[i] - '0') * TEN
                    + (str[j] - '0');
  
            // If any subsequence of two
            // digits is divisible by 4
            if (num % 4 == 0)
            {
                res = Math.min(res, num);
            }
        }
    }
  
    return ((res == Integer.MAX_VALUE) ? -1 : res);
}
  
// Driver code
public static void main(String[] args)
{
    String str = "17";
    int len = str.length();
  
    System.out.print(minNum(str.toCharArray(), len));
  
}
}
  
// This code is contributed by 29AjayKumar


Python 3
# Python3 implementation of the approach
import sys
TEN = 10
  
# Function to return the minimum number
# that can be formed after removing
# the digits which is a multiple of 4
def minNum(str, len1):
    res = sys.maxsize
  
    # For every digit of the number
    for i in range(len1):
          
        # Check if the current digit
        # is divisible by 4
        if (str[i] == '4' or str[i] == '8'):
            res = min(res, ord(str[i]) - ord('0'))
  
    for i in range(len1 - 1):
        for j in range(i + 1, len1, 1):
            num = (ord(str[i]) - ord('0')) * TEN + \
                  (ord(str[j]) - ord('0'))
  
            # If any subsequence of two
            # digits is divisible by 4
            if (num % 4 == 0):
                res = min(res, num)
  
    if (res == sys.maxsize):
        return -1
    else:
        return res
  
# Driver code
if __name__ == '__main__':
    str = "17"
    len1 = len(str)
  
    print(minNum(str, len1))
      
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the approach
using System;
  
class GFG
{
static int TEN = 10;
  
// Function to return the minimum number
// that can be formed after removing
// the digits which is a multiple of 4
static int minNum(char[] str, int len)
{
    int res = int.MaxValue;
  
    // For every digit of the number
    for (int i = 0; i < len; i++)
    {
  
        // Check if the current digit
        // is divisible by 4
        if (str[i] == '4' || str[i] == '8')
        {
            res = Math.Min(res, str[i] - '0');
        }
    }
  
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = i + 1; j < len; j++) 
        {
            int num = (str[i] - '0') * TEN
                    + (str[j] - '0');
  
            // If any subsequence of two
            // digits is divisible by 4
            if (num % 4 == 0)
            {
                res = Math.Min(res, num);
            }
        }
    }
    return ((res == int.MaxValue) ? -1 : res);
}
  
// Driver code
public static void Main(String[] args)
{
    String str = "17";
    int len = str.Length;
  
    Console.Write(minNum(str.ToCharArray(), len));
}
}
  
// This code is contributed by Rajput-Ji


输出:
-1