给定最大可能对角线数时多边形的边数
给定一个数字K ,它是任何多边形中可能的最大对角线数,任务是找到该多边形的边数。
例子:
Input: 2
Output: 4
Exlanation: A quadrilateral has 2 diagonals at max.
Input: 6
Output: -1
Explanation: No polygon has at most 6 diagonals.
方法:这个问题可以根据以下数学观察来解决:
直觉:
for an n-sided convex polygon, from each vertex, we can draw (n – 3) diagonals.
Following this way for n-vertices, there will be n*(n – 3) diagonals but each diagonal is calculated twice.
So the total number of diagonals become n*(n – 3)/2
Therefore:
n*(n – 3) / 2 = K
n*(n – 3) – 2 *K = 0
n2 – 3*n -2*K = 0
When this is compared with the general representation of quadratic equation (ax2 + bx + c)
a = 1, b = -3, c = -2 *K
for this equation.
Solve the equation to get the value of n which is:
n = (-b ± √(b2 – 4ac) )/ 2a
按照下面提到的步骤来实现这个想法:
- 使用上面推导的公式找到n的值:
- 判别式d = b 2 – 4ac
- 如果d > 0:根将是不同的,并且正根之一将是答案
- 如果d == 0:根将相等,这就是答案
- 如果d < 0:根将是虚构的并且答案不存在
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the roots of quadratic equation
int findRoots(int K)
{
int a = 1;
int b = -3;
int c = -2 * K;
// Finding discriminant
int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));
// Root are distinct
if (d > 0) {
// roots of equation
double x1 = (-b + sqrt_val) / (2 * a);
double x2 = (-b - sqrt_val) / (2 * a);
if ((int)x1 == x1 && x1 > 0)
return (int(x1));
else if ((int)x2 == x2 && x2 > 0)
return (int(x2));
else
return -1;
}
// Roots are equal
else if (d == 0) {
// roots of equation
double x1 = (-b / (2 * a));
if ((int)x1 == x1 && x1 > 0)
return (int(x1));
else
return -1;
}
// Root are imaginary
else
return -1;
}
// Driver code
int main()
{
// K is number of diagonals
int K = 9;
// Function call
cout << findRoots(K);
return 0;
}
// This code is contributed by Rohit Pradhan
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the roots of quadratic equation
static double findRoots(int K){
int a = 1;
int b = -3;
int c = -2 * K;
// Finding discriminant
int d = b * b - 4 * a * c;
double sqrt_val = Math.sqrt(Math.abs(d));
// Root are distinct
if (d > 0) {
// roots of equation
double x1 = (-b + sqrt_val) / (2 * a);
double x2 = (-b - sqrt_val) / (2 * a);
if ((int)x1 == x1 && x1 > 0)
return x1;
else if ((int)x2 == x2 && x2 > 0)
return x2;
else
return -1;
}
// Roots are equal
else if (d == 0)
{
// roots of equation
double x1 = (-b / (2 * a));
if ((int)x1 == x1 && x1 > 0)
return x1;
else
return -1;
}
// Root are imaginary
else
return -1;
}
// Driver code
public static void main (String[] args)
{
// K is number of diagonals
int K = 9;
// Function call
System.out.println((int) findRoots(K));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python3 program for the above approach
import math
# Function to find the roots of quadratic equation
def findRoots(K):
a = 1
b = -3
c = -2 * K
# Finding discriminant
d = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(d))
# Root are distinct
if d > 0:
# roots of equation
x1 = (-b + sqrt_val)/(2 * a)
x2 = (-b - sqrt_val)/(2 * a)
if int(x1) == x1 and x1 > 0:
return (int(x1))
elif int(x2) == x2 and x2 > 0:
return (int(x2))
else:
return -1
# Roots are equal
elif d == 0:
# roots of equation
x1 = (-b / (2 * a))
if int(x1) == x1 and x1 > 0:
return (int(x1))
else:
return -1
# Root are imaginary
else:
return -1
# Driver code
if __name__ == '__main__':
# K is number of diagonals
K = 9
# Function call
print(findRoots(K))
Javascript
6
时间复杂度: O(1)
辅助空间: O(1)