给定一个n边规则多边形和三个顶点a1 , a2和a3 ,任务是找到由顶点a2和顶点a3悬在顶点a1处的角度。
例子:
Input: n = 6, a1 = 1, a2 = 2, a3 = 4
Output: 90
Input: n = 5, a1 = 1, a2 = 2, a3 = 5
Output: 36
方法:
- n边正多边形的中心上的边缘与边缘所成的角度为360 / n 。
- 由k个边分开的顶点对着的角度变为(360 * k)/ n 。
- 顶点之间的弦所对角的角度等于在第三顶点的中心处的角度值的一半,该第三顶点是外接圆的圆周上的一个点。
- 令以此方式获得的角度为a =(180 * x)/ n ,其中k为i和k之间的边数。
- 类似地,对于相反的顶点,我们得到的角度为b =(180 * y)/ n ,其中l是j和k之间的边数。
- 因此,三个顶点之间的角度等于180-ab 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that checks whether given angle
// can be created using any 3 sides
double calculate_angle(int n, int i, int j, int k)
{
// Initialize x and y
int x, y;
// Calculate the number of vertices
// between i and j, j and k
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
// Calculate the angle subtended
// at the circumference
double ang1 = (180 * x) / n;
double ang2 = (180 * y) / n;
// Angle subtended at j can be found
// using the fact that the sum of angles
// of a triangle is equal to 180 degrees
double ans = 180 - ang1 - ang2;
return ans;
}
// Driver code
int main()
{
int n = 5;
int a1 = 1;
int a2 = 2;
int a3 = 5;
cout << calculate_angle(n, a1, a2, a3);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function that checks whether given angle
// can be created using any 3 sides
static double calculate_angle(int n, int i,
int j, int k)
{
// Initialize x and y
int x, y;
// Calculate the number of vertices
// between i and j, j and k
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
// Calculate the angle subtended
// at the circumference
double ang1 = (180 * x) / n;
double ang2 = (180 * y) / n;
// Angle subtended at j can be found
// using the fact that the sum of angles
// of a triangle is equal to 180 degrees
double ans = 180 - ang1 - ang2;
return ans;
}
// Driver code
public static void main (String[] args)
{
int n = 5;
int a1 = 1;
int a2 = 2;
int a3 = 5;
System.out.println((int)calculate_angle(n, a1, a2, a3));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function that checks whether given angle
# can be created using any 3 sides
def calculate_angle(n, i, j, k):
# Initialize x and y
x, y = 0, 0
# Calculate the number of vertices
# between i and j, j and k
if (i < j):
x = j - i
else:
x = j + n - i
if (j < k):
y = k - j
else:
y = k + n - j
# Calculate the angle subtended
# at the circumference
ang1 = (180 * x) // n
ang2 = (180 * y) // n
# Angle subtended at j can be found
# using the fact that the sum of angles
# of a triangle is equal to 180 degrees
ans = 180 - ang1 - ang2
return ans
# Driver code
n = 5
a1 = 1
a2 = 2
a3 = 5
print(calculate_angle(n, a1, a2, a3))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that checks whether given angle
// can be created using any 3 sides
static double calculate_angle(int n, int i,
int j, int k)
{
// Initialize x and y
int x, y;
// Calculate the number of vertices
// between i and j, j and k
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
// Calculate the angle subtended
// at the circumference
double ang1 = (180 * x) / n;
double ang2 = (180 * y) / n;
// Angle subtended at j can be found
// using the fact that the sum of angles
// of a triangle is equal to 180 degrees
double ans = 180 - ang1 - ang2;
return ans;
}
// Driver code
public static void Main ()
{
int n = 5;
int a1 = 1;
int a2 = 2;
int a3 = 5;
Console.WriteLine((int)calculate_angle(n, a1, a2, a3));
}
}
// This code is contributed by ihritik
Javascript
输出:
36