📜  为每个不分割该元素的任何数字的数组元素打印数字

📅  最后修改于: 2021-09-06 05:51:17             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,每个数组元素arr[i]的任务是从[0, 9]中找到所有不分割arr[i] 中的任何数字的数字。

例子:

处理方法:按照以下步骤解决问题:

  • 遍历给定的数组arr[]并执行以下步骤:
    • 使用变量i迭代范围[2, 9] ,如果元素arr[i]中不存在任何可被i整除的数字,则打印数字i
    • 否则,继续下一次迭代。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find digits for each array
// element that doesn't divide any digit
// of the that element
void indivisibleDigits(int arr[], int N)
{
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        int num = 0;
 
        cout << arr[i] << ": ";
 
        // Iterate over the range [2, 9]
        for (int j = 2; j < 10; j++) {
 
            int temp = arr[i];
 
            // Stores if there exists any digit
            // in arr[i] which is divisible by j
            bool flag = true;
 
            while (temp > 0) {
 
                // If any digit of the number
                // is divisible by j
                if ((temp % 10) != 0
                    && (temp % 10) % j == 0) {
 
                    flag = false;
                    break;
                }
 
                temp /= 10;
            }
 
            // If the digit j doesn't
            // divide any digit of arr[i]
            if (flag) {
                cout << j << ' ';
            }
        }
 
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 4162, 1152, 99842 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    indivisibleDigits(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to find digits for each array
  // element that doesn't divide any digit
  // of the that element
  static void indivisibleDigits(int[] arr, int N)
  {
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
      System.out.print(arr[i] + ": ");
 
      // Iterate over the range [2, 9]
      for (int j = 2; j < 10; j++)
      {
        int temp = arr[i];
 
        // Stores if there exists any digit
        // in arr[i] which is divisible by j
        boolean flag = true;
        while (temp > 0) {
 
          // If any digit of the number
          // is divisible by j
          if ((temp % 10) != 0
              && (temp % 10) % j == 0) {
 
            flag = false;
            break;
          }
 
          temp /= 10;
        }
 
        // If the digit j doesn't
        // divide any digit of arr[i]
        if (flag) {
          System.out.print(j + " ");
        }
      }
 
      System.out.println();
    }
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 4162, 1152, 99842 };
    int N = arr.length;
 
    indivisibleDigits(arr, N);
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python3 program for the above approach
 
# Function to find digits for each array
# element that doesn't divide any digit
# of the that element
def indivisibleDigits(arr, N) :
     
    # Traverse the array arr[]
    for i in range(N):
 
        num = 0
 
        print(arr[i], end = ' ')
 
        # Iterate over the range [2, 9]
        for j in range(2, 10):
 
            temp = arr[i]
 
            # Stores if there exists any digit
            # in arr[i] which is divisible by j
            flag = True
 
            while (temp > 0) :
 
                # If any digit of the number
                # is divisible by j
                if ((temp % 10) != 0
                    and (temp % 10) % j == 0) :
 
                    flag = False
                    break
                 
 
                temp //= 10
             
 
            # If the digit j doesn't
            # divide any digit of arr[i]
            if (flag) :
                print(j, end = ' ')
 
        print()
     
# Driver Code
 
arr = [ 4162, 1152, 99842 ]
N = len(arr)
 
indivisibleDigits(arr, N)
 
# This code is contributed by susmitakundugoaldanga.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find digits for each array
  // element that doesn't divide any digit
  // of the that element
  static void indivisibleDigits(int[] arr, int N)
  {
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
      Console.Write(arr[i] + ": ");
 
      // Iterate over the range [2, 9]
      for (int j = 2; j < 10; j++)
      {
        int temp = arr[i];
 
        // Stores if there exists any digit
        // in arr[i] which is divisible by j
        bool flag = true;
        while (temp > 0) {
 
          // If any digit of the number
          // is divisible by j
          if ((temp % 10) != 0
              && (temp % 10) % j == 0) {
 
            flag = false;
            break;
          }
 
          temp /= 10;
        }
 
        // If the digit j doesn't
        // divide any digit of arr[i]
        if (flag) {
          Console.Write(j + " ");
        }
      }
 
      Console.WriteLine();
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 4162, 1152, 99842 };
    int N = arr.Length;
 
    indivisibleDigits(arr, N);
  }
}
 
// This code is contributed by rishavmahato348.


Javascript


输出:
4162: 5 7 8 9 
1152: 3 4 6 7 8 9 
99842: 5 6 7

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live