📌  相关文章
📜  检查通过将两个数字相乘形成的数字是否为Perfect Cube

📅  最后修改于: 2021-04-29 13:10:44             🧑  作者: Mango

给定两个数字ab ,任务是检查ab的串联是否为完美的立方体。
例子:

方法:由于附加字符串比附加数字要容易得多,因此解决此问题的最简单方法是将给定的整数转换为字符串。一旦获得了连接数,就可以很容易地检查它是否是理想的立方体。
下面是上述方法的实现。

C++
// C++ program to check if the
// concatenation of two numbers
// is a perfect cube or not
 
#include 
using namespace std;
 
// Function to check if a number is
// a perfect Cube or not
bool isPerfectCube(int x)
{
    long double cr = round(cbrt(x));
 
    return (cr * cr * cr == x);
}
 
// Function to check if
// concatenation of two numbers
// is a perfect cube or not
void checkCube(int a, int b)
{
 
    // Convert numbers to string
    // using to_string()
    string s1 = to_string(a);
    string s2 = to_string(b);
 
    // Concatenate the numbers and
    // convert it into integer
    int c = stoi(s1 + s2);
 
    // Check if concatenated value
    // is perfect cube or not
    if (isPerfectCube(c)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    int a = 6;
    int b = 4;
 
    checkCube(a, b);
 
    return 0;
}


Java
// Java program to check if the
// concatenation of two numbers
// is a perfect cube or not
class GFG {
     
    // Function to check if a number is
    // a perfect Cube or not
    static boolean isPerfectCube(int x)
    {
        long cr = Math.round(Math.cbrt(x));
     
        return (cr * cr * cr == x);
    }
     
    // Function to check if
    // concatenation of two numbers
    // is a perfect cube or not
    static void checkCube(int a, int b)
    {
     
        // Convert numbers to string
        // using to_string()
        String s1 = Integer.toString(a);
        String s2 = Integer.toString(b);
     
        // Concatenate the numbers and
        // convert it into integer
        int c = Integer.parseInt(s1 + s2);
     
        // Check if concatenated value
        // is perfect cube or not
        if (isPerfectCube(c)) {
                System.out.println("Yes");
        }
        else {
                System.out.println("No");
        }
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int a = 6;
        int b = 4;
     
        checkCube(a, b);  
    }
}
 
// This code is contributed by Yash_R


Python 3
# Python 3 program to check if the
# concatenation of two numbers
# is a perfect cube or not
 
# Function to check if a number is
# a perfect Cube or not
def isPerfectCube(x):
    x = abs(x)
    return int(round(x ** (1. / 3))) ** 3 == x
 
# Function to check if
# concatenation of two numbers
# is a perfect cube or not
def checkCube(a, b):
 
    # Convert numbers to string
    # using to_string()
    s1 = str(a)
    s2 = str(b)
 
    # Concatenate the numbers and
    # convert it into integer
    c = int(s1 + s2)
 
    # Check if concatenated value
    # is perfect cube or not
    if (isPerfectCube(c)):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
    a = 6
    b = 4
 
    checkCube(a, b)
     
# This code is contributed by Surendra_Gangwar


C#
// C# program to check if the
// concatenation of two numbers
// is a perfect cube or not
using System;
 
class GFG {
     
    // Function to check if a number is
    // a perfect Cube or not
    static bool isPerfectCube(int x)
    {
        double cr = Math.Round(Math.Cbrt(x));
     
        return (cr * cr * cr == x);
    }
     
    // Function to check if
    // concatenation of two numbers
    // is a perfect cube or not
    static void checkCube(int a, int b)
    {
     
        // Convert numbers to string
        // using to_string()
        string s1 = Convert.ToString(a);
        string s2 = Convert.ToString(b);
     
        // Concatenate the numbers and
        // convert it into integer
        int c = Convert.ToInt32(s1 + s2);
     
        // Check if concatenated value
        // is perfect cube or not
        if (isPerfectCube(c)) {
                Console.WriteLine("Yes");
        }
        else {
                Console.WriteLine("No");
        }
    }
     
    // Driver Code
    public static void Main ()
    {
        int a = 6;
        int b = 4;
     
        checkCube(a, b);
    }
}
 
// This code is contributed by AbhiThakur


Javascript


输出:
Yes

时间复杂度: O(cbrt(concate(a,b)))

辅助空间: O(1)