📌  相关文章
📜  数组中最大的完美立方数

📅  最后修改于: 2022-05-13 01:57:05.200000             🧑  作者: Mango

数组中最大的完美立方数

给定一个包含N个整数的数组。任务是找到最大的数,它是一个完美的立方体。如果没有完美立方体的数字,则打印 -1。
例子

Input : arr[] = {16, 8, 25, 2, 3, 10} 
Output : 25
Explanation: 25 is the largest number 
that is a perfect cube. 

Input : arr[] = {36, 64, 10, 16, 29, 25| 
Output : 64

一个简单的解决方案是对元素进行排序并对N个数字进行排序,然后使用 cbrt()函数从后面开始检查完美的立方体数。倒数第一个完美立方数就是我们的答案。排序的复杂度是 O(n log n),而 cbrt()函数的复杂度是 log n,所以在最坏的情况下复杂度是 O(n log n)。
一个有效的解决方案是迭代 O(n) 中的所有元素并每次与最大元素进行比较,并存储所有完美立方体的最大值。
下面是上述方法的实现:

C++
// CPP program to find the largest perfect
// cube number among n numbers
 
#include 
using namespace std;
 
// Function to check if a number
// is perfect cube number or not
bool checkPerfectcube(int n)
{
    // takes the sqrt of the number
    int d = cbrt(n);
 
    // checks if it is a perfect
    // cube number
    if (d * d * d == n)
        return true;
 
    return false;
}
 
// Function to find the largest perfect
// cube number in the array
int largestPerfectcubeNumber(int a[], int n)
{
    // stores the maximum of all
    // perfect cube numbers
    int maxi = -1;
 
    // Traverse all elements in the array
    for (int i = 0; i < n; i++) {
 
        // store the maximum if current
        // element is a perfect cube
        if (checkPerfectcube(a[i]))
            maxi = max(a[i], maxi);
    }
 
    return maxi;
}
 
// Driver Code
int main()
{
    int a[] = { 16, 64, 25, 2, 3, 10 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << largestPerfectcubeNumber(a, n);
 
    return 0;
}


Java
// Java program to find the largest perfect
// cube number among n numbers
class Solution
{
 
// Function to check if a number
// is perfect cube number or not
static boolean checkPerfectcube(int n)
{
    // takes the sqrt of the number
    int d =(int) Math.cbrt(n);
 
    // checks if it is a perfect
    // cube number
    if (d * d * d == n)
        return true;
 
    return false;
}
 
// Function to find the largest perfect
// cube number in the array
static int largestPerfectcubeNumber(int a[], int n)
{
    // stores the maximum of all
    // perfect cube numbers
    int maxi = -1;
 
    // Traverse all elements in the array
    for (int i = 0; i < n; i++) {
 
        // store the maximum if current
        // element is a perfect cube
        if (checkPerfectcube(a[i]))
            maxi = Math.max(a[i], maxi);
    }
 
    return maxi;
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 16, 64, 25, 2, 3, 10 };
 
    int n =a.length;
 
    System.out.print(largestPerfectcubeNumber(a, n));
 
}
}
 
//contributed by Arnab Kundu


Python3
# Python 3 program to find the largest
# perfect cube number among n numbers
import math
 
# Function to check if a number
# is perfect cube number or not
def checkPerfectcube(n):
     
    # checks if it is a perfect
    # cube number
    cube_root = n**(1./3.)
    if round(cube_root) ** 3 == n:
        return True
         
    else:
        return False
 
# Function to find the largest perfect
# cube number in the array
def largestPerfectcubeNumber(a, n):
     
    # stores the maximum of all
    # perfect cube numbers
    maxi = -1
 
    # Traverse all elements in the array
    for i in range(0, n, 1):
         
        # store the maximum if current
        # element is a perfect cube
        if (checkPerfectcube(a[i])):
            maxi = max(a[i], maxi)
 
    return maxi;
 
# Driver Code
if __name__ == '__main__':
    a = [16, 64, 25, 2, 3, 10]
 
    n = len(a)
 
    print(largestPerfectcubeNumber(a, n))
 
# This code is contributed by
# Surendra_Gangwar


C#
//C# program to find the largest perfect
// cube number among n numbers
using System;
 
public class Solution
{
 
    // Function to check if a number
    // is perfect cube number or not
    static bool checkPerfectcube(int n)
    {
        // takes the sqrt of the number
        int d = (int)Math.Ceiling(Math.Pow(n, (double)1 / 3));
        // checks if it is a perfect
        // cube number
        if (d * d * d == n)
            return true;
 
        return false;
    }
 
    // Function to find the largest perfect
    // cube number in the array
    static int largestPerfectcubeNumber(int []a, int n)
    {
        // stores the maximum of all
        // perfect cube numbers
        int maxi = -1;
 
        // Traverse all elements in the array
        for (int i = 0; i < n; i++) {
 
            // store the maximum if current
            // element is a perfect cube
            if (checkPerfectcube(a[i]))
                maxi = Math.Max(a[i], maxi);
        }
 
        return maxi;
    }
 
    // Driver Code
    public static void Main()
    {
        int []a = { 16, 64, 25, 2, 3, 10 };
 
        int n =a.Length;
 
        Console.WriteLine(largestPerfectcubeNumber(a, n));
 
    }
}
 
/*This code is contributed by PrinciRaj1992*/


PHP


Javascript


输出:
64