在 N 边正多边形中找到三个顶点,使得一个顶点与其他两个顶点所对的角度最接近 A
给定一个N边正凸多边形和一个以度为单位的角度A ,任务是找到任意3个顶点i 、 j和k ,使得顶点i和k在顶点j对向的角度最接近A 。
注意:每个顶点从任何顶点开始按逆时针方向从1到N编号。
例子:
Input: N = 3, A = 15
Output: 2 1 3
Explanation:
The given polygon is a triangle, therefore the minimum angle that can be subtended on any vertex is equal to 30 which is closest to A and includes all the vertices of the polygon.
Input: N = 4, A = 90
Output: 2 1 4
方法:可以根据以下观察解决给定的问题:
- 正多边形的顶点位于一个圆上。
- n 边正多边形的中心角为360/N 。
- 由X边分隔的顶点对圆心的角度等于(360*X)/N 。
- 由定理,圆弧所对的外接圆的角是同一弧所对中心角的一半。因此,由X边形成的弧对顶点的角度将等于(180*X)/N 。
- 由于多边形是规则的,因此可以通过取任意2个边差等于X的顶点来对向相同的角度。
请按照以下步骤解决问题:
- 初始化两个变量,比如ans为0和minElement为INT_MAX以存储与A最接近的角度的边数,并分别存储最近的角度。
- 使用变量i迭代范围[1, N – 2]并执行以下步骤:
- 使用上述公式求出由i条边形成的弧所对外接圆的角度,并将其存储在变量angle 中。
- 检查(A – angle)的绝对值是否小于(minElement – A)的绝对值,然后将i的值更新为ans ,将angle的值更新为minElement 。
- 完成上述步骤后,打印顶点{2, 1, 2 + ans}作为结果顶点。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define ll long long
#define MAXN 1000000
// Function to find three vertices that
// subtends an angle closest to A
void closestsAngle(int N, int A)
{
// Stores the closest angle to A
double mi = INT_MAX;
// Stores the count of edge which
// subtend an angle of A
int ans = 0;
// Iterate in the range [1, N-2]
for (int i = 1; i < N - 1; i++) {
// Stores the angle subtended
double angle = 180.0 * i / N;
// If absolute(angle-A) is less
// than absolute(mi-A)
if (fabs(angle - A) < fabs(mi - A)) {
// Update angle to mi, and
// also update i to ans
mi = angle;
ans = i;
}
}
// Print the vertices
cout << 2 << ' ' << 1 << ' '
<< 2 + ans;
}
// Driver Code
int main()
{
int N = 3, A = 15;
closestsAngle(N, A);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find three vertices that
// subtends an angle closest to A
static void closestsAngle(int N, int A)
{
// Stores the closest angle to A
double mi = Integer.MAX_VALUE;
// Stores the count of edge which
// subtend an angle of A
int ans = 0;
// Iterate in the range [1, N-2]
for (int i = 1; i < N - 1; i++) {
// Stores the angle subtended
double angle = 180.0 * i / N;
// If absolute(angle-A) is less
// than absolute(mi-A)
if (Math.abs(angle - A) < Math.abs(mi - A)) {
// Update angle to mi, and
// also update i to ans
mi = angle;
ans = i;
}
}
// Print the vertices
System.out.print(2 + " " + 1 + " " + (2 + ans));
}
// Driver Code
public static void main(String[] args)
{
int N = 3, A = 15;
closestsAngle(N, A);
}
}
// This code is contributed by subhammahato348.
Python3
# Python program for the above approach
# Function to find three vertices that
# subtends an angle closest to A
import math
import sys
def closestsAngle(N, A):
# Stores the closest angle to A
mi = sys.maxsize
# Stores the count of edge which
# subtend an angle of A
ans = 0
# Iterate in the range [1, N-2]
for i in range(N ):
# Stores the angle subtended
angle = 180.0 * i / N
# If absolute(angle-A) is less
# than absolute(mi-A)
if (math.fabs(angle - A) < math.fabs(mi - A)):
# Update angle to mi, and
# also update i to ans
mi = angle
i+=1
ans = i
# Pr the vertices
print(2 , 1 , 2 + ans)
# Driver Code
if __name__ == '__main__':
N = 3
A = 15
closestsAngle(N, A)
# This code is contributed by Shivani
C#
// C# program for the above approach
using System;
class GFG{
// Function to find three vertices that
// subtends an angle closest to A
static void closestsAngle(int N, int A)
{
// Stores the closest angle to A
double mi = Int32.MaxValue;
// Stores the count of edge which
// subtend an angle of A
int ans = 0;
// Iterate in the range [1, N-2]
for(int i = 1; i < N - 1; i++)
{
// Stores the angle subtended
double angle = 180.0 * i / N;
// If absolute(angle-A) is less
// than absolute(mi-A)
if (Math.Abs(angle - A) <
Math.Abs(mi - A))
{
// Update angle to mi, and
// also update i to ans
mi = angle;
ans = i;
}
}
// Print the vertices
Console.Write(2 + " " + 1 +
" " + (2 + ans));
}
// Driver Code
public static void Main()
{
int N = 3, A = 15;
closestsAngle(N, A);
}
}
// This code is contributed by subhammahato348
Javascript
输出:
2 1 3
时间复杂度: O(N)
辅助空间: O(1)