📜  从数字字符串删除字符,以使字符串可以被8整除

📅  最后修改于: 2021-06-26 09:06:44             🧑  作者: Mango

给定一个以数字字符串str形式表示的非负整数。从字符串删除零个或多个字符,以使数字可被8整除。如果有可能,请在删除字符后打印字符串,否则打印-1

例子:

方法:考虑8的除数规则,我们只需要检查str的后3个字符形成的数字是否可被8整除。因此,我们可以遍历8的所有倍数直到1000,并检查给定字符串是否有任何倍数作为子序列存在,那么该倍数就是我们所需的答案。否则,将没有答案,因为所有大于8的8的倍数也都需要具有已经检查过的数字(由最后3位数字组成)。

下面是上述方法的实现:

C++
// C++ program to remove digits from a
// numeric string such that the number
// becomes divisible by 8
#include 
using namespace std;
  
// Function that return true if sub
// is a sub-sequence in s
int checkSub(string sub, string s)
{
    int j = 0;
    for (int i = 0; i < s.size(); i++)
        if (sub[j] == s[i])
            j++;
    return j == (int)sub.size();
}
  
// Function to return a multiple of 8
// formed after removing 0 or more characters
// from the given string
int getMultiple(string s)
{
    // Iterate over all multiples of 8
    for (int i = 0; i < 1e3; i += 8) {
  
        // If current multiple
        // exists as a subsequence
        // in the given string
        if (checkSub(to_string(i), s))
            return i;
    }
  
    return -1;
}
  
// Driver Code
int main()
{
    string s = "3454";
    cout << getMultiple(s);
  
    return 0;
}


Java
// Java program to remove digits from a 
// numeric string such that the number 
// becomes divisible by 8 
  
class GFG
{
  
    // Function that return true if sub 
    // is a sub-sequence in s 
    static boolean checkSub(String sub, String s) 
    { 
        int j = 0; 
        for (int i = 0; i < s.length(); i++) 
            if (sub.charAt(j) == s.charAt(i)) 
                j++; 
                  
        return j == sub.length(); 
    } 
      
    // Function to return a multiple of 8 
    // formed after removing 0 or more characters 
    // from the given string 
    static int getMultiple(String s) 
    { 
        // Iterate over all multiples of 8 
        for (int i = 0; i < 1E3; i += 8) 
        { 
      
            // If current multiple 
            // exists as a subsequence 
            // in the given string 
            if (checkSub(Integer.toString(i), s)) 
                return i; 
        } 
        return -1; 
    } 
  
    // Driver Code 
    public static void main (String[] args) 
    {
            String s = "3454"; 
            System.out.println(getMultiple(s)); 
    }
}
  
// This code is contributed by mits


Python3
# Python3 program to remove digits from 
# a numeric string such that the number
# becomes divisible by 8
import math as mt
  
# Function that return true if sub
# is a sub-sequence in s
def checkSub(sub, s):
  
    j = 0
    for i in range(len(s)):
        if (sub[j] == s[i]):
            j += 1
  
    if j == int(len(sub)):
        return True
    else:
        return False
  
# Function to return a multiple of 8
# formed after removing 0 or more 
# characters from the given string
def getMultiple(s):
      
    # Iterate over all multiples of 8
    for i in range(0, 10**3, 8):
  
        # If current multiple
        # exists as a subsequence
        # in the given string
        if (checkSub(str(i), s)):
            return i
  
    return -1
  
# Driver Code
s = "3454"
print(getMultiple(s))
  
# This code is contributed
# by Mohit Kumar 29


C#
// C# program to remove digits from a 
// numeric string such that the number 
// becomes divisible by 8 
using System;
  
class GFG
{
  
    // Function that return true if sub 
    // is a sub-sequence in s 
    static bool checkSub(string sub, string s) 
    { 
        int j = 0; 
        for (int i = 0; i < s.Length; i++) 
            if (sub[j] == s[i]) 
                j++; 
                  
        return j == sub.Length; 
    } 
      
    // Function to return a multiple of 8 
    // formed after removing 0 or more characters 
    // from the given string 
    static int getMultiple(string s) 
    { 
        // Iterate over all multiples of 8 
        for (int i = 0; i < 1e3; i += 8) 
        { 
      
            // If current multiple 
            // exists as a subsequence 
            // in the given string 
            if (checkSub(i.ToString(), s)) 
                return i; 
        } 
        return -1; 
    } 
  
    // Driver Code 
    static void Main()
    {
            string s = "3454"; 
            Console.WriteLine(getMultiple(s)); 
    }
}
  
// This code is contributed by Ryuga


PHP


输出:
344

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。