原文:

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

示例:

输入: arr[] = {4162,1152,99842} 输出: 4162->5 7 8 9 1152->3 4 6 7 8 9 99842->5 6 7 解释: 为 arr[0] ( = 4162): 无位数 对于 arr: 元素 1152 的所有数字都不能被 9、8、7、6、4、3 整除。 对于 arr: 元素 99842 的所有数字都不能被 7、6、5 整除。

输入:arr[]= { 2021 } t3】输出:t5】2021->3 4 5 6 7 8 9

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

  • 遍历给定数组 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.

python 3

# 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.

java 描述语言


output: 

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

时间复杂度:o(10 * n * log10n) t8】辅助空间: o(1)