给定一个n边正多边形和一个角度θ,任务是找出在正n个多边形(正多边形)中角度(A i ,A j ,A k )=θ(i
例子:
Input: n = 4, ang = 90
Output: 4
Input: n = 6, ang = 50
Output: 0
方法:
- 首先,我们检查这种角度是否存在。
- 考虑顶点为x , y和z ,找到的角度为∠xyz。
- x和y之间的边数为a , y和z之间的边数为b 。
- 然后∠xyz = 180 –(180 *(a + b))/ n 。
- 因此∠xyz * n(mod 180)= 0 。
- 接下来,我们需要找到这种角度的数量。
- 由于多边形是规则的,因此我们只需要计算一个顶点的角度数,就可以将结果直接乘以n(顶点数)。
- 在每个顶点处的角度可以在n-1-freq次数处找到,其中freq =(n * ang)/ 180 ,它描述了创建所需角度后剩余的边数,即z和x之间的边数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that calculates occurrences
// of given angle that can be created
// using any 3 sides
int solve(int ang, int n)
{
// Maximum angle in a regular n-gon
// is equal to the interior angle
// If the given angle
// is greater than the interior angle
// then the given angle cannot be created
if ((ang * n) > (180 * (n - 2))) {
return 0;
}
// The given angle times n should be divisible
// by 180 else it cannot be created
else if ((ang * n) % 180 != 0) {
return 0;
}
// Initialise answer
int ans = 1;
// Calculate the frequency
// of given angle for each vertex
int freq = (ang * n) / 180;
// Multiply answer by frequency.
ans = ans * (n - 1 - freq);
// Multiply answer by the number of vertices.
ans = ans * n;
return ans;
}
// Driver code
int main()
{
int ang = 90, n = 4;
cout << solve(ang, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function that calculates occurrences
// of given angle that can be created
// using any 3 sides
static int solve(int ang, int n)
{
// Maximum angle in a regular n-gon
// is equal to the interior angle
// If the given angle
// is greater than the interior angle
// then the given angle cannot be created
if ((ang * n) > (180 * (n - 2)))
{
return 0;
}
// The given angle times n should be divisible
// by 180 else it cannot be created
else if ((ang * n) % 180 != 0)
{
return 0;
}
// Initialise answer
int ans = 1;
// Calculate the frequency
// of given angle for each vertex
int freq = (ang * n) / 180;
// Multiply answer by frequency.
ans = ans * (n - 1 - freq);
// Multiply answer by the number of vertices.
ans = ans * n;
return ans;
}
// Driver code
public static void main (String[] args)
{
int ang = 90, n = 4;
System.out.println(solve(ang, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function that calculates occurrences
# of given angle that can be created
# using any 3 sides
def solve(ang, n):
# Maximum angle in a regular n-gon
# is equal to the interior angle
# If the given angle
# is greater than the interior angle
# then the given angle cannot be created
if ((ang * n) > (180 * (n - 2))):
return 0
# The given angle times n should be divisible
# by 180 else it cannot be created
elif ((ang * n) % 180 != 0):
return 0
# Initialise answer
ans = 1
# Calculate the frequency
# of given angle for each vertex
freq = (ang * n) // 180
# Multiply answer by frequency.
ans = ans * (n - 1 - freq)
# Multiply answer by the number of vertices.
ans = ans * n
return ans
# Driver code
ang = 90
n = 4
print(solve(ang, n))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that calculates occurrences
// of given angle that can be created
// using any 3 sides
static int solve(int ang, int n)
{
// Maximum angle in a regular n-gon
// is equal to the interior angle
// If the given angle
// is greater than the interior angle
// then the given angle cannot be created
if ((ang * n) > (180 * (n - 2)))
{
return 0;
}
// The given angle times n should be divisible
// by 180 else it cannot be created
else if ((ang * n) % 180 != 0)
{
return 0;
}
// Initialise answer
int ans = 1;
// Calculate the frequency
// of given angle for each vertex
int freq = (ang * n) / 180;
// Multiply answer by frequency.
ans = ans * (n - 1 - freq);
// Multiply answer by the
// number of vertices.
ans = ans * n;
return ans;
}
// Driver code
public static void Main (String[] args)
{
int ang = 90, n = 4;
Console.WriteLine(solve(ang, n));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
4