📌  相关文章
📜  以所有元素为裸数的数组中最长子序列的长度

📅  最后修改于: 2021-05-14 01:29:47             🧑  作者: Mango

给定N个正整数的数组arr [] ,任务是打印该数组最长子序列的长度,以使它的所有元素均为Nude Numbers。

例子:

方法:要解决此问题,请执行以下步骤:

  • 遍历给定的数组以及该数组中的每个元素,然后检查它是否为Nude数字。
  • 如果元素是Nude Number ,则它将包含在结果最长的子序列中。因此,将子序列中元素的数量增加1
  • 完成上述步骤后,打印计数值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the number
// is a Nude number
bool isNudeNum(int n)
{
    // Variable initialization
    int copy, length, flag = 0;
    copy = n;
    string temp;
 
    // Integer 'copy' is converted
    // to a string
    temp = to_string(copy);
 
    // Total digits in the number
    length = temp.length();
 
    // Loop through all digits and check
    // if every digit divides n or not
    for (int i = 0; i < length; i++) {
 
        int num = temp[i] - '0';
 
        if (num == 0 or n % num != 0) {
 
            // flag is used to keep check
            flag = 1;
        }
    }
 
    // Return true or false as per
    // the condition
    if (flag == 1)
        return false;
 
    else
        return true;
}
 
// Function to find the longest subsequence
// which contain all Nude numbers
int longestNudeSubseq(int arr[], int n)
{
    int answer = 0;
 
    // Find the length of longest
    // Nude number subsequence
    for (int i = 0; i < n; i++) {
        if (isNudeNum(arr[i]))
            answer++;
    }
    return answer;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 34, 34, 2, 2, 3,
                  333, 221, 32 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << longestNudeSubseq(arr, n)
         << endl;
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if the number
// is a Nude number
static boolean isNudeNum(int n)
{
     
    // Variable initialization
    int copy, length, flag = 0;
    copy = n;
    String temp;
 
    // Integer 'copy' is converted
    // to a String
    temp = String.valueOf(copy);
 
    // Total digits in the number
    length = temp.length();
 
    // Loop through all digits and check
    // if every digit divides n or not
    for(int i = 0; i < length; i++)
    {
        int num = temp.charAt(i) - '0';
 
        if (num == 0 || n % num != 0)
        {
             
            // flag is used to keep check
            flag = 1;
        }
    }
 
    // Return true or false as per
    // the condition
    if (flag == 1)
        return false;
    else
        return true;
}
 
// Function to find the longest subsequence
// which contain all Nude numbers
static int longestNudeSubseq(int arr[], int n)
{
    int answer = 0;
 
    // Find the length of longest
    // Nude number subsequence
    for(int i = 0; i < n; i++)
    {
        if (isNudeNum(arr[i]))
            answer++;
    }
    return answer;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array arr[]
    int arr[] = { 34, 34, 2, 2, 3,
                  333, 221, 32 };
 
    int n = arr.length;
 
    // Function call
    System.out.print(longestNudeSubseq(arr, n) + "\n");
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program for the above approach
 
# Function to check if the number
# is a Nude number
def isNudeNum(n):
     
    # Variable initialization
    flag = 0
    copy = n
 
    # Integer 'copy' is converted
    # to a string
    temp = str(copy)
 
    # Total digits in the number
    length = len(temp)
 
    # Loop through all digits and check
    # if every digit divides n or not
    for i in range(length):
        num = ord(temp[i]) - ord('0')
 
        if ((num == 0) or (n % num != 0)):
 
            # flag is used to keep check
            flag = 1
         
    # Return true or false as per
    # the condition
    if (flag == 1):
        return False
    else:
        return True
 
# Function to find the longest subsequence
# which contain all Nude numbers
def longestNudeSubseq(arr, n):
     
    answer = 0
 
    # Find the length of longest
    # Nude number subsequence
    for i in range(n):
        if (isNudeNum(arr[i])):
            answer += 1
     
    return answer
 
# Driver Code
 
# Given array arr[]
arr = [ 34, 34, 2, 2, 3,
        333, 221, 32 ]
 
n = len(arr)
 
# Function call
print(longestNudeSubseq(arr, n))
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the number
// is a Nude number
static bool isNudeNum(int n)
{
     
    // Variable initialization
    int copy, length, flag = 0;
    copy = n;
    String temp;
 
    // int 'copy' is converted
    // to a String
    temp = String.Join("", copy);
 
    // Total digits in the number
    length = temp.Length;
 
    // Loop through all digits and check
    // if every digit divides n or not
    for(int i = 0; i < length; i++)
    {
        int num = temp[i] - '0';
 
        if (num == 0 || n % num != 0)
        {
             
            // flag is used to keep check
            flag = 1;
        }
    }
 
    // Return true or false as per
    // the condition
    if (flag == 1)
        return false;
    else
        return true;
}
 
// Function to find the longest subsequence
// which contain all Nude numbers
static int longestNudeSubseq(int []arr, int n)
{
    int answer = 0;
 
    // Find the length of longest
    // Nude number subsequence
    for(int i = 0; i < n; i++)
    {
        if (isNudeNum(arr[i]))
            answer++;
    }
    return answer;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []arr = { 34, 34, 2, 2, 3,
                  333, 221, 32 };
 
    int n = arr.Length;
 
    // Function call
    Console.Write(longestNudeSubseq(arr, n) + "\n");
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
4

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