📌  相关文章
📜  检查是否有大量的排列被8整除

📅  最后修改于: 2021-06-26 17:27:16             🧑  作者: Mango

给定一个大数N,任务是检查大数的排列是否可被8整除。

例子:

Input: N = 31462708
Output: Yes
Many of permutation of number N like 
34678120, 34278160 are divisible by 8. 

Input: 75
Output: No

一个简单的方法是生成数字N的所有排列并检查if(N%8 == 0),如果任何排列可被8整除,则返回true。

一种有效的方法是利用以下事实:如果数字的后三位数字可以被8整除,那么该数字也可以被8整除。以下是必需的步骤:

  • 使用哈希表计算给定数字中所有数字的出现次数。
  • 遍历所有可被8整除的三位数。
  • 检查三位数字中的数字是否在哈希表中。
  • 如果是,则可以通过排列形成一个数字,其中最后三个数字合并形成三个数字。
  • 如果三位数都不可以形成,则返回false。

下面是上述方法的实现:

C++
// C++ program to check if any permutation of
// a large number is divisible by 8 or not
#include 
using namespace std;
  
// Function to check if any permutation
// of a large number is divisible by 8
bool solve(string n, int l)
{
  
    // Less than three digit number
    // can be checked directly.
    if (l < 3) {
        if (stoi(n) % 8 == 0)
            return true;
  
        // check for the reverse of a number
        reverse(n.begin(), n.end());
        if (stoi(n) % 8 == 0)
            return true;
        return false;
    }
  
    // Stores the Frequency of characters in the n.
    int hash[10] = { 0 };
    for (int i = 0; i < l; i++)
        hash[n[i] - '0']++;
  
    // Iterates for all three digit numbers
    // divisible by 8
    for (int i = 104; i < 1000; i += 8) {
  
        int dup = i;
  
        // stores the frequency of all single
        // digit in three-digit number
        int freq[10] = { 0 };
        freq[dup % 10]++;
        dup = dup / 10;
        freq[dup % 10]++;
        dup = dup / 10;
        freq[dup % 10]++;
  
        dup = i;
  
        // check if the original number has
        // the digit
        if (freq[dup % 10] > hash[dup % 10])
            continue;
        dup = dup / 10;
  
        if (freq[dup % 10] > hash[dup % 10])
            continue;
        dup = dup / 10;
  
        if (freq[dup % 10] > hash[dup % 10])
            continue;
  
        return true;
    }
  
    // when all are checked its not possible
    return false;
}
  
// Driver Code
int main()
{
    string number = "31462708";
    int l = number.length();
  
    if (solve(number, l))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program to check if 
// any permutation of a large
// number is divisible by 8 or not
import java.util.*;
  
class GFG
{
    // Function to check if any 
    // permutation of a large 
    // number is divisible by 8
    public static boolean solve(String n, int l)
    {
          
        // Less than three digit number
        // can be checked directly.
        if (l < 3) 
        {
            if (Integer.parseInt(n) % 8 == 0)
                return true;
      
            // check for the reverse
            // of a number
            n = new String((new StringBuilder()).append(n).reverse());
              
            if (Integer.parseInt(n) % 8 == 0)
                return true;
            return false;
        }
      
        // Stores the Frequency of
        // characters in the n.
        int []hash = new int[10];
        for (int i = 0; i < l; i++)
            hash[n.charAt(i) - '0']++;
      
        // Iterates for all 
        // three digit numbers
        // divisible by 8
        for (int i = 104; i < 1000; i += 8) 
        {
            int dup = i;
      
            // stores the frequency of 
            // all single digit in 
            // three-digit number
            int []freq = new int[10];
            freq[dup % 10]++;
            dup = dup / 10;
            freq[dup % 10]++;
            dup = dup / 10;
            freq[dup % 10]++;
      
            dup = i;
      
            // check if the original 
            // number has the digit
            if (freq[dup % 10] > 
                hash[dup % 10])
                continue;
            dup = dup / 10;
      
            if (freq[dup % 10] > 
                hash[dup % 10])
                continue;
            dup = dup / 10;
      
            if (freq[dup % 10] > 
                hash[dup % 10])
                continue;
      
            return true;
        }
      
        // when all are checked 
        // its not possible
        return false;
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        String number = "31462708";
          
        int l = number.length();
      
        if (solve(number, l))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed
// by Harshit Saini


Python3
# Python3 program to check if 
# any permutation of a large
# number is divisible by 8 or not
  
# Function to check if any 
# permutation of a large 
# number is divisible by 8
def solve(n, l):
      
    # Less than three digit 
    # number can be checked 
    # directly.
    if l < 3:
        if int(n) % 8 == 0:
            return True
          
        # check for the reverse 
        # of a number
        n = n[::-1]
  
          
        if int(n) % 8 == 0:
            return True
        return False
  
    # Stores the Frequency of
    # characters in the n.
    hash = 10 * [0]
    for i in range(0, l):
        hash[int(n[i]) - 0] += 1;
  
    # Iterates for all 
    # three digit numbers
    # divisible by 8
    for i in range(104, 1000, 8):
        dup = i
  
        # stores the frequency 
        # of all single digit 
        # in three-digit number
        freq = 10 * [0]
        freq[int(dup % 10)] += 1;
        dup = dup / 10
        freq[int(dup % 10)] += 1;
        dup = dup / 10
        freq[int(dup % 10)] += 1;
          
        dup = i
          
        # check if the original 
        # number has the digit
        if (freq[int(dup % 10)] > 
            hash[int(dup % 10)]):
            continue;
        dup = dup / 10;
          
        if (freq[int(dup % 10)] > 
            hash[int(dup % 10)]):
            continue;
        dup = dup / 10
          
        if (freq[int(dup % 10)] > 
            hash[int(dup % 10)]):
            continue;
          
        return True
  
    # when all are checked
    # its not possible
    return False
      
# Driver Code
if __name__ == "__main__":
      
    number = "31462708"
      
    l = len(number)
      
    if solve(number, l):
        print("Yes")
    else:
        print("No")
          
# This code is contributed
# by Harshit Saini


C#
// C# program to check if 
// any permutation of a large
// number is divisible by 8 or not
using System;
using System.Collections.Generic; 
      
class GFG
{
    // Function to check if any 
    // permutation of a large 
    // number is divisible by 8
    public static bool solve(String n, int l)
    {
          
        // Less than three digit number
        // can be checked directly.
        if (l < 3) 
        {
            if (int.Parse(n) % 8 == 0)
                return true;
      
            // check for the reverse
            // of a number
            n = reverse(n);
              
            if (int.Parse(n) % 8 == 0)
                return true;
            return false;
        }
      
        // Stores the Frequency of
        // characters in the n.
        int []hash = new int[10];
        for (int i = 0; i < l; i++)
            hash[n[i] - '0']++;
      
        // Iterates for all 
        // three digit numbers
        // divisible by 8
        for (int i = 104; i < 1000; i += 8) 
        {
            int dup = i;
      
            // stores the frequency of 
            // all single digit in 
            // three-digit number
            int []freq = new int[10];
            freq[dup % 10]++;
            dup = dup / 10;
            freq[dup % 10]++;
            dup = dup / 10;
            freq[dup % 10]++;
      
            dup = i;
      
            // check if the original 
            // number has the digit
            if (freq[dup % 10] > 
                hash[dup % 10])
                continue;
            dup = dup / 10;
      
            if (freq[dup % 10] > 
                hash[dup % 10])
                continue;
            dup = dup / 10;
      
            if (freq[dup % 10] > 
                hash[dup % 10])
                continue;
      
            return true;
        }
      
        // when all are checked 
        // its not possible
        return false;
    }
      
    static String reverse(String input) 
    {
        char[] a = input.ToCharArray();
        int l, r = 0;
        r = a.Length - 1;
  
        for (l = 0; l < r; l++, r--) 
        {
            // Swap values of l and r 
            char temp = a[l];
            a[l] = a[r];
            a[r] = temp;
        }
        return String.Join("",a);
    }
      
    // Driver Code
    public static void Main(String[] args)
    {
        String number = "31462708";
          
        int l = number.Length;
      
        if (solve(number, l))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by Princi Singh


PHP
 
            $hash[$dup % 10])
            continue;
        $dup = $dup / 10;
  
        if ($freq[$dup % 10] > 
            $hash[$dup % 10])
            continue;
        $dup = $dup / 10;
  
        if ($freq[$dup % 10] > 
            $hash[$dup % 10])
            continue;
  
        return true;
    }
  
    // when all are checked 
    // its not possible
    return false;
}
  
// Driver Code
$number = "31462708";
$l = strlen($number);
  
if (solve($number, $l))
    echo "Yes";
else
    echo "No";
  
// This code is contributed
// by Akanksha Rai(Abby_akku)


输出:
Yes

时间复杂度: O(L),其中L是数字中的位数。

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