📌  相关文章
📜  使用Binary Search检查给定数字是否为完美平方

📅  最后修改于: 2021-04-26 19:22:36             🧑  作者: Mango

检查给定的数字N是否是一个完美的平方。如果是,则返回它是一个完美正方形的数字,否则打印-1。

例子:

方法:为解决上述问题,我们将使用二进制搜索算法。

  • 从起始值和最后一个值中找到mid元素,然后将mid(mid * mid)的平方值与N进行比较。
  • 如果相等,则返回中值,否则检查square(mid * mid)是否大于N,然后以相同的起始值但最后更改为mid-1值的方式进行递归调用,并且square(mid * mid)小于然后,N的递归调用具有相同的最后一个值,但更改了起始值。
  • 如果N不是平方根,则返回-1。

下面是上述方法的实现:

C++
// C++ program to check if a
// given number is Perfect
// square using Binary Search
 
#include 
using namespace std;
 
// function to check for
// perfect square number
int checkPerfectSquare(
    long int N,
    long int start,
    long int last)
{
    // Find the mid value
    // from start and last
    long int mid = (start + last) / 2;
 
    if (start > last) {
        return -1;
    }
 
    // check if we got the number which
    // is square root of the perfect
    // square number N
    if (mid * mid == N) {
        return mid;
    }
 
    // if the square(mid) is greater than N
    // it means only lower values then mid
    // will be possibly the square root of N
    else if (mid * mid > N) {
        return checkPerfectSquare(
            N, start, mid - 1);
    }
 
    // if the square(mid) is less than N
    // it means only higher values then mid
    // will be possibly the square root of N
    else {
        return checkPerfectSquare(
            N, mid + 1, last);
    }
}
 
// Driver code
int main()
{
    long int N = 65;
 
    cout << checkPerfectSquare(N, 1, N);
    return 0;
}


Java
// Java program to check if a
// given number is Perfect
// square using Binary Search
import java.util.*;
 
class GFG {
 
// Function to check for
// perfect square number
static int checkPerfectSquare(long N,
                              long start,
                              long last)
{
    // Find the mid value
    // from start and last
    long mid = (start + last) / 2;
 
    if (start > last)
    {
        return -1;
    }
 
    // Check if we got the number which
    // is square root of the perfect
    // square number N
    if (mid * mid == N)
    {
        return (int)mid;
    }
 
    // If the square(mid) is greater than N
    // it means only lower values then mid
    // will be possibly the square root of N
    else if (mid * mid > N)
    {
        return checkPerfectSquare(N, start,
                                  mid - 1);
    }
 
    // If the square(mid) is less than N
    // it means only higher values then mid
    // will be possibly the square root of N
    else
    {
        return checkPerfectSquare(N, mid + 1,
                                  last);
    }
}
 
// Driver code
public static void main(String[] args)
{
    long N = 65;
    System.out.println(checkPerfectSquare(N, 1, N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to check if a
# given number is perfect
# square using Binary Search
 
# Function to check for
# perfect square number
def checkPerfectSquare(N, start, last):
 
    # Find the mid value
    # from start and last
    mid = int((start + last) / 2)
 
    if (start > last):
        return -1
 
    # Check if we got the number which
    # is square root of the perfect
    # square number N
    if (mid * mid == N):
        return mid
 
    # If the square(mid) is greater than N
    # it means only lower values then mid
    # will be possibly the square root of N
    elif (mid * mid > N):
        return checkPerfectSquare(N, start,
                                  mid - 1)
 
    # If the square(mid) is less than N
    # it means only higher values then mid
    # will be possibly the square root of N
    else:
        return checkPerfectSquare(N, mid + 1,
                                  last)
 
# Driver code
N = 65
print (checkPerfectSquare(N, 1, N))
 
# This code is contributed by PratikBasu


C#
// C# program to check if a
// given number is Perfect
// square using Binary Search
using System;
 
class GFG{
 
// Function to check for
// perfect square number
public static int checkPerfectSquare(int N,
                                     int start,
                                     int last)
{
    // Find the mid value
    // from start and last
    int mid = (start + last) / 2;
 
    if (start > last)
    {
        return -1;
    }
 
    // Check if we got the number which
    // is square root of the perfect
    // square number N
    if (mid * mid == N)
    {
        return mid;
    }
 
    // If the square(mid) is greater than N
    // it means only lower values then mid
    // will be possibly the square root of N
    else if (mid * mid > N)
    {
        return checkPerfectSquare(N, start,
                                  mid - 1);
    }
 
    // If the square(mid) is less than N
    // it means only higher values then mid
    // will be possibly the square root of N
    else
    {
        return checkPerfectSquare(N, mid + 1,
                                  last);
    }
}
 
// Driver code
public static int Main()
{
    int N = 65;
 
    Console.Write(checkPerfectSquare(N, 1, N));
    return 0;
}
}
 
// This code is contributed by sayesha


Javascript


输出:
-1

时间复杂度: O(登录)