📜  检查所有子编号是否都有不同的数字乘积

📅  最后修改于: 2021-04-21 22:53:04             🧑  作者: Mango

给定数字N,任务是检查此数字的所有子数字是否具有不同的数字乘积。

注意事项

  • N位数字具有N *(N + 1)/ 2个子数字。例如,所有975的可能子编号是9、7、5、97、75、975。
  • 数字的乘积是其数字的乘积。

例子:

Input : N = 324
Output : YES
Sub-numbers of 324 are 3, 2, 4, 32, 24 and 324 
and digit products are 3, 2, 4, 6, 8 and 24 
respectively. All the digit products are different.

Input : N = 323
Output : NO
Sub-numbers of 323 are 3, 2, 3, 32, 23 and 323
and digit products are 3, 2, 3, 6, 6 and 18 
respectively. Digit products 3 and 6 have occurred 
twice. 

方法 :

  1. 制作一个数字数组,即以其元素为给定数字N的数字组成的数组。
  2. 现在找到N的子数类似于找到数字数组的所有可能的子数组。
  3. 维护这些子阵列的数字积的列表。
  4. 如果任何数字产品出现不止一次,请打印“否”。
  5. 否则打印“是”。

下面是上述方法的实现:

C++
// C++ program to check if all sub-numbers
// have distinct Digit product
#include
using namespace std;
  
  
// Function to calculate product of
// digits between given indexes
int digitProduct(int digits[], int start, int end)
{
    int pro = 1;
    for (int i = start; i <= end; i++) {
        pro *= digits[i];
    }
    return pro;
}
  
// Function to check if all sub-numbers
// have distinct Digit product
bool isDistinct(int N)
{
    string s = to_string(N);
      
    // Length of number N
    int len = s.length();
  
    // Digit array
    int digits[len];
  
    // set to maintain digit products
    unordered_set products;
  
    for (int i = 0; i < len; i++) {
        digits[i] = s[i]-'0';
    }
  
    // Finding all possible subarrays
    for (int i = 0; i < len; i++) {
        for (int j = i; j < len; j++) {
  
            int val = digitProduct(digits, i, j);
  
            if (products.find(val)!=products.end())
                return false;
            else
                products.insert(val);
        }
    }
  
    return true;
}
  
// Driver code
int main()
{
    int N = 324;
  
    if (isDistinct(N))
        cout << "YES";
    else
        cout << "NO";
          
    return 0;
}


Java
// Java program to check if all sub-numbers
// have distinct Digit product
import java.io.*;
import java.util.*;
  
public class GFG {
  
    // Function to calculate product of
    // digits between given indexes
    static int digitProduct(int[] digits, int start, int end)
    {
        int pro = 1;
        for (int i = start; i <= end; i++) {
            pro *= digits[i];
        }
        return pro;
    }
  
    // Function to check if all sub-numbers
    // have distinct Digit product
    static boolean isDistinct(int N)
    {
        String s = "" + N;
  
        // Length of number N
        int len = s.length();
  
        // Digit array
        int[] digits = new int[len];
  
        // List to maintain digit products
        ArrayList products = new ArrayList<>();
  
        for (int i = 0; i < len; i++) {
            digits[i] = Integer.parseInt("" + s.charAt(i));
        }
  
        // Finding all possible subarrays
        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
  
                int val = digitProduct(digits, i, j);
  
                if (products.contains(val))
                    return false;
                else
                    products.add(val);
            }
        }
  
        return true;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int N = 324;
  
        if (isDistinct(N))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


Python3
# Python3 program to check if all 
# sub-numbers have distinct Digit product 
  
# Function to calculate product of 
# digits between given indexes 
def digitProduct(digits, start, end): 
  
    pro = 1
    for i in range(start, end + 1): 
        pro *= digits[i] 
      
    return pro 
  
# Function to check if all sub-numbers 
# have distinct Digit product 
def isDistinct(N): 
  
    s = str(N) 
      
    # Length of number N 
    length = len(s) 
  
    # Digit array 
    digits = [None] * length
  
    # set to maintain digit products 
    products = set()
  
    for i in range(0, length): 
        digits[i] = int(s[i])
      
    # Finding all possible subarrays 
    for i in range(0, length): 
        for j in range(i, length): 
  
            val = digitProduct(digits, i, j) 
      
            if val in products: 
                return False
            else:
                products.add(val) 
          
    return True
  
# Driver Code
if __name__ == "__main__": 
  
    N = 324
  
    if isDistinct(N) == True: 
        print("YES") 
    else:
        print("NO") 
          
# This code is contributed 
# by Rituraj Jain


C#
// C# program to check if all sub-numbers
// have distinct Digit product
  
using System;
using System.Collections;
using System.Collections.Generic;
public class GFG {
  
    // Function to calculate product of
    // digits between given indexes
    static int digitProduct(int[] digits, int start, int end)
    {
        int pro = 1;
        for (int i = start; i <= end; i++) {
            pro *= digits[i];
        }
        return pro;
    }
  
    // Function to check if all sub-numbers
    // have distinct Digit product
    static bool isDistinct(int N)
    {
        string s = N.ToString();
  
        // Length of number N
        int len = s.Length;
  
        // Digit array
        int[] digits = new int[len];
  
        // List to maintain digit products
        ArrayList products = new ArrayList();
  
        for (int i = 0; i < len; i++) {
            digits[i] = s[i]-'0';
        }
  
        // Finding all possible subarrays
        for (int i = 0; i < len; i++) {
            for (int j = i; j < len; j++) {
  
                int val = digitProduct(digits, i, j);
  
                if (products.Contains(val))
                    return false;
                else
                    products.Add(val);
            }
        }
  
        return true;
    }
  
    // Driver code
    public static void Main()
    {
        int N = 324;
  
        if (isDistinct(N))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
// This code is contributed by ihritik


PHP


输出:
YES