给定一个整数 x,求它的平方根。如果 x 不是完全平方数,则返回 floor(√x)。
例子 :
Input: x = 4
Output: 2
Explanation: The square root of 4 is 2.
Input: x = 11
Output: 3
Explanation: The square root of 11 lies in between
3 and 4 so floor of the square root is 3.
可以有很多方法来解决这个问题。例如巴比伦方法是一种方法。
简单方法:要找到平方根的底数,请尝试使用从 1 开始的全自然数。继续递增该数,直到该数的平方大于给定数。
- 算法:
- 创建一个变量(计数器) i并处理一些基本情况,即当给定的数字是 0 或 1 时。
- 运行循环直到i*i <= n ,其中 n 是给定的数字。将 i 增加 1。
- 数的平方根的底数是i – 1
- 执行:
C++
// A C++ program to find floor(sqrt(x)
#include
using namespace std;
// Returns floor of square root of x
int floorSqrt(int x)
{
// Base cases
if (x == 0 || x == 1)
return x;
// Staring from 1, try all numbers until
// i*i is greater than or equal to x.
int i = 1, result = 1;
while (result <= x)
{
i++;
result = i * i;
}
return i - 1;
}
// Driver program
int main()
{
int x = 11;
cout << floorSqrt(x) << endl;
return 0;
}
Java
// A Java program to find floor(sqrt(x))
class GFG {
// Returns floor of square root of x
static int floorSqrt(int x)
{
// Base cases
if (x == 0 || x == 1)
return x;
// Staring from 1, try all numbers until
// i*i is greater than or equal to x.
int i = 1, result = 1;
while (result <= x) {
i++;
result = i * i;
}
return i - 1;
}
// Driver program
public static void main(String[] args)
{
int x = 11;
System.out.print(floorSqrt(x));
}
}
// This code is contributed by Smitha Dinesh Semwal.
Python3
# Python3 program to find floor(sqrt(x)
# Returns floor of square root of x
def floorSqrt(x):
# Base cases
if (x == 0 or x == 1):
return x
# Staring from 1, try all numbers until
# i*i is greater than or equal to x.
i = 1; result = 1
while (result <= x):
i += 1
result = i * i
return i - 1
# Driver Code
x = 11
print(floorSqrt(x))
# This code is contributed by Smitha Dinesh Semwal.
C#
// A C# program to
// find floor(sqrt(x))
using System;
class GFG
{
// Returns floor of
// square root of x
static int floorSqrt(int x)
{
// Base cases
if (x == 0 || x == 1)
return x;
// Staring from 1, try all
// numbers until i*i is
// greater than or equal to x.
int i = 1, result = 1;
while (result <= x)
{
i++;
result = i * i;
}
return i - 1;
}
// Driver Code
static public void Main ()
{
int x = 11;
Console.WriteLine(floorSqrt(x));
}
}
// This code is contributed by ajit
PHP
Javascript
C++
// A C++ program to find floor(sqrt(x)
#include
using namespace std;
// Returns floor of square root of x
int floorSqrt(int x)
{
// Base cases
if (x == 0 || x == 1)
return x;
// Do Binary Search for floor(sqrt(x))
int start = 1, end = x, ans;
while (start <= end) {
int mid = (start + end) / 2;
// If x is a perfect square
if (mid * mid == x)
return mid;
// Since we need floor, we update answer when
// mid*mid is smaller than x, and move closer to
// sqrt(x)
/*
if(mid*mid<=x)
{
start = mid+1;
ans = mid;
}
Here basically if we multiply mid with itself so
there will be integer overflow which will throw
tle for larger input so to overcome this
situation we can use long or we can just divide
the number by mid which is same as checking
mid*mid < x
*/
if (mid <= x / mid) {
start = mid + 1;
ans = mid;
}
else // If mid*mid is greater than x
end = mid - 1;
}
return ans;
}
// Driver program
int main()
{
int x = 11;
cout << floorSqrt(x) << endl;
return 0;
}
Java
// A Java program to find floor(sqrt(x)
public class Test
{
public static int floorSqrt(int x)
{
// Base Cases
if (x == 0 || x == 1)
return x;
// Do Binary Search for floor(sqrt(x))
long start = 1, end = x, ans=0;
while (start <= end)
{
int mid = (start + end) / 2;
// If x is a perfect square
if (mid*mid == x)
return (int)mid;
// Since we need floor, we update answer when mid*mid is
// smaller than x, and move closer to sqrt(x)
if (mid*mid < x)
{
start = mid + 1;
ans = mid;
}
else // If mid*mid is greater than x
end = mid-1;
}
return (int)ans;
}
// Driver Method
public static void main(String args[])
{
int x = 11;
System.out.println(floorSqrt(x));
}
}
// Contributed by InnerPeace
Python3
# Python 3 program to find floor(sqrt(x)
# Returns floor of square root of x
def floorSqrt(x) :
# Base cases
if (x == 0 or x == 1) :
return x
# Do Binary Search for floor(sqrt(x))
start = 1
end = x
while (start <= end) :
mid = (start + end) // 2
# If x is a perfect square
if (mid*mid == x) :
return mid
# Since we need floor, we update
# answer when mid*mid is smaller
# than x, and move closer to sqrt(x)
if (mid * mid < x) :
start = mid + 1
ans = mid
else :
# If mid*mid is greater than x
end = mid-1
return ans
# driver code
x = 11
print(floorSqrt(x))
# This code is contributed by Nikita Tiwari.
C#
// A C# program to
// find floor(sqrt(x)
using System;
class GFG
{
public static int floorSqrt(int x)
{
// Base Cases
if (x == 0 || x == 1)
return x;
// Do Binary Search
// for floor(sqrt(x))
int start = 1, end = x, ans = 0;
while (start <= end)
{
int mid = (start + end) / 2;
// If x is a
// perfect square
if (mid * mid == x)
return mid;
// Since we need floor, we
// update answer when mid *
// mid is smaller than x,
// and move closer to sqrt(x)
if (mid * mid < x)
{
start = mid + 1;
ans = mid;
}
// If mid*mid is
// greater than x
else
end = mid-1;
}
return ans;
}
// Driver Code
static public void Main ()
{
int x = 11;
Console.WriteLine(floorSqrt(x));
}
}
// This code is Contributed by m_kit
PHP
Javascript
输出 :
3
- 复杂度分析:
- 时间复杂度: O(√ n)。
只需要遍历一次解,所以时间复杂度为O(√n)。 - 空间复杂度: O(1)。
需要不断的额外空间。
- 时间复杂度: O(√ n)。
感谢 Fattepur Mahesh 提出这个解决方案。
更好的方法:这个想法是找到平方小于或等于给定数字的最大整数i 。这个想法是使用二分搜索来解决问题。 i * i 的值是单调递增的,所以这个问题可以用二分查找来解决。
- 算法:
- 注意一些基本情况,即当给定的数字是 0 或 1 时。
- 创建一些变量,下界l = 0 ,上界r = n ,其中 n 是给定的数字, mid和ans来存储答案。
- 运行一个循环直到l <= r ,搜索空间消失
- 检查mid( mid=(l+r)/2 )的平方是否小于等于n,如果是则在搜索空间的后半部分寻找更大的值,即l=mid+1,更新ans=中
- 否则如果 mid 的平方大于 n 则在搜索空间的前半部分搜索较小的值,即 r = mid – 1
- 打印 answer ( ans ) 的值
- 执行:
C++
// A C++ program to find floor(sqrt(x)
#include
using namespace std;
// Returns floor of square root of x
int floorSqrt(int x)
{
// Base cases
if (x == 0 || x == 1)
return x;
// Do Binary Search for floor(sqrt(x))
int start = 1, end = x, ans;
while (start <= end) {
int mid = (start + end) / 2;
// If x is a perfect square
if (mid * mid == x)
return mid;
// Since we need floor, we update answer when
// mid*mid is smaller than x, and move closer to
// sqrt(x)
/*
if(mid*mid<=x)
{
start = mid+1;
ans = mid;
}
Here basically if we multiply mid with itself so
there will be integer overflow which will throw
tle for larger input so to overcome this
situation we can use long or we can just divide
the number by mid which is same as checking
mid*mid < x
*/
if (mid <= x / mid) {
start = mid + 1;
ans = mid;
}
else // If mid*mid is greater than x
end = mid - 1;
}
return ans;
}
// Driver program
int main()
{
int x = 11;
cout << floorSqrt(x) << endl;
return 0;
}
Java
// A Java program to find floor(sqrt(x)
public class Test
{
public static int floorSqrt(int x)
{
// Base Cases
if (x == 0 || x == 1)
return x;
// Do Binary Search for floor(sqrt(x))
long start = 1, end = x, ans=0;
while (start <= end)
{
int mid = (start + end) / 2;
// If x is a perfect square
if (mid*mid == x)
return (int)mid;
// Since we need floor, we update answer when mid*mid is
// smaller than x, and move closer to sqrt(x)
if (mid*mid < x)
{
start = mid + 1;
ans = mid;
}
else // If mid*mid is greater than x
end = mid-1;
}
return (int)ans;
}
// Driver Method
public static void main(String args[])
{
int x = 11;
System.out.println(floorSqrt(x));
}
}
// Contributed by InnerPeace
蟒蛇3
# Python 3 program to find floor(sqrt(x)
# Returns floor of square root of x
def floorSqrt(x) :
# Base cases
if (x == 0 or x == 1) :
return x
# Do Binary Search for floor(sqrt(x))
start = 1
end = x
while (start <= end) :
mid = (start + end) // 2
# If x is a perfect square
if (mid*mid == x) :
return mid
# Since we need floor, we update
# answer when mid*mid is smaller
# than x, and move closer to sqrt(x)
if (mid * mid < x) :
start = mid + 1
ans = mid
else :
# If mid*mid is greater than x
end = mid-1
return ans
# driver code
x = 11
print(floorSqrt(x))
# This code is contributed by Nikita Tiwari.
C#
// A C# program to
// find floor(sqrt(x)
using System;
class GFG
{
public static int floorSqrt(int x)
{
// Base Cases
if (x == 0 || x == 1)
return x;
// Do Binary Search
// for floor(sqrt(x))
int start = 1, end = x, ans = 0;
while (start <= end)
{
int mid = (start + end) / 2;
// If x is a
// perfect square
if (mid * mid == x)
return mid;
// Since we need floor, we
// update answer when mid *
// mid is smaller than x,
// and move closer to sqrt(x)
if (mid * mid < x)
{
start = mid + 1;
ans = mid;
}
// If mid*mid is
// greater than x
else
end = mid-1;
}
return ans;
}
// Driver Code
static public void Main ()
{
int x = 11;
Console.WriteLine(floorSqrt(x));
}
}
// This code is Contributed by m_kit
PHP
Javascript
输出 :
3
- 复杂度分析:
- 时间复杂度: O(log n)。
二分查找的时间复杂度是 O(log n)。 - 空间复杂度: O(1)。
需要不断的额外空间。
- 时间复杂度: O(log n)。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。