给定数字N ,找到需要添加到N或从N减去的最小数,以使其成为一个完美的平方。如果要加数字,则用+号打印;否则,如果要减数字,则用-号打印。
例子:
Input: N = 14
Output: 2
Nearest perfect square before 14 = 9
Nearest perfect square after 14 = 16
Therefore 2 needs to be added to 14 to get the closest perfect square
Input: N = 18
Output: -2
Nearest perfect square before 18 = 16
Nearest perfect square after 18 = 25
Therefore 2 needs to be subtracted from 18 to get the closest perfect square
方法:
- 获取号码。
- 找到数字的平方根,然后将结果转换为整数。
- 将double值转换为整数后,它将包含N之前的完美平方的根,即floor(square root(N)) 。
- 然后找到该数字的平方,它将是N之前的完美平方。
- 在N之后找到理想平方的根,即ceil(square root(N)) 。
- 然后找到该数字的平方,它将是N之后的完美平方。
- 检查底值的平方是否最接近N或ceil值。
- 如果底值的平方最接近N,则用-符号打印差异。否则,打印出ceil值的平方和带有+号的N之间的差。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the Least number
int nearest(int n)
{
// Get the perfect square
// before and after N
int prevSquare = sqrt(n);
int nextSquare = prevSquare + 1;
prevSquare = prevSquare * prevSquare;
nextSquare = nextSquare * nextSquare;
// Check which is nearest to N
int ans
= (n - prevSquare) < (nextSquare - n)
? (prevSquare - n)
: (nextSquare - n);
// return the result
return ans;
}
// Driver code
int main()
{
int n = 14;
cout << nearest(n) << endl;
n = 16;
cout << nearest(n) << endl;
n = 18;
cout << nearest(n) << endl;
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the Least number
static int nearest(int n)
{
// Get the perfect square
// before and after N
int prevSquare = (int)Math.sqrt(n);
int nextSquare = prevSquare + 1;
prevSquare = prevSquare * prevSquare;
nextSquare = nextSquare * nextSquare;
// Check which is nearest to N
int ans = (n - prevSquare) < (nextSquare - n)? (prevSquare - n): (nextSquare - n);
// return the result
return ans;
}
// Driver code
public static void main (String[] args)
{
int n = 14;
System.out.println(nearest(n));
n = 16;
System.out.println(nearest(n)) ;
n = 18;
System.out.println(nearest(n)) ;
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
from math import sqrt
# Function to return the Least number
def nearest(n) :
# Get the perfect square
# before and after N
prevSquare = int(sqrt(n));
nextSquare = prevSquare + 1;
prevSquare = prevSquare * prevSquare;
nextSquare = nextSquare * nextSquare;
# Check which is nearest to N
ans = (prevSquare - n) if (n - prevSquare) < (nextSquare - n) else (nextSquare - n);
# return the result
return ans;
# Driver code
if __name__ == "__main__" :
n = 14;
print(nearest(n)) ;
n = 16;
print(nearest(n));
n = 18;
print(nearest(n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the Least number
static int nearest(int n)
{
// Get the perfect square
// before and after N
int prevSquare = (int)Math.Sqrt(n);
int nextSquare = prevSquare + 1;
prevSquare = prevSquare * prevSquare;
nextSquare = nextSquare * nextSquare;
// Check which is nearest to N
int ans = (n - prevSquare) < (nextSquare - n)? (prevSquare - n): (nextSquare - n);
// return the result
return ans;
}
// Driver code
public static void Main (string[] args)
{
int n = 14;
Console.WriteLine(nearest(n));
n = 16;
Console.WriteLine(nearest(n)) ;
n = 18;
Console.WriteLine(nearest(n)) ;
}
}
// This code is contributed by AnkitRai01
输出:
2
0
-2