📌  相关文章
📜  使用n边正多边形的3个顶点形成的给定角度的出现次数

📅  最后修改于: 2021-04-27 05:03:32             🧑  作者: Mango

给定一个n边正多边形和一个角度θ,任务是找出在正n个多边形(正多边形)中角度(A i ,A j ,A k )=θ(i 1 ,A 2 ,…,A n
例子:

Input: n = 4, ang = 90
Output: 4

Input: n = 6, ang = 50
Output: 0

方法:

  1. 首先,我们检查这种角度是否存在。
  2. 考虑顶点为xyz ,找到的角度为∠xyz。
  3. xy之间的边数为ayz之间的边数为b
  4. 然后∠xyz = 180 –(180 *(a + b))/ n
  5. 因此∠xyz * n(mod 180)= 0
  6. 接下来,我们需要找到这种角度的数量。
  7. 由于多边形是规则的,因此我们只需要计算一个顶点的角度数,就可以将结果直接乘以n(顶点数)。
  8. 在每个顶点处的角度可以在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