📜  给定初始角度和每角度增量的多边形的第 N 个角度

📅  最后修改于: 2021-10-27 06:18:32             🧑  作者: Mango

给定四个整数N、A、K、n ,其中N表示多边形的边数, A表示多边形的初始角度, K表示每个角度的增加,任务是找到具有N的多边形的n角度双方。如果不可能,则打印-1

例子:

方法:这个想法是观察多边形的角度在算术级数方面增加了K度的差异。

  • 找出N 边多边形的角和和给定多边形的角之和。
  • 检查两个值是否相等。如果是,那么第n 个角度是可能的,因此找到第n 个角度
  • 否则,打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the angle
// is possible or not
bool possible(int N, int a, int b, int n)
{
    // Angular sum of a N-sided polygon
    int sum_of_angle = 180 * (N - 2);
 
    // Angular sum of N-sided given polygon
    int Total_angle = (N * ((2 * a) + (N - 1) * b)) / 2;
 
    // Check if both sum are equal
    if (sum_of_angle != Total_angle)
        return false;
    else
        return true;
}
 
// Function to find the nth angle
int nth_angle(int N, int a,
              int b, int n)
{
    int nth = 0;
 
    // Calculate nth angle
    nth = a + (n - 1) * b;
 
    // Return the nth angle
    return nth;
}
 
// Driver Code
int main()
{
 
    int N = 3, a = 30, b = 30, n = 3;
 
    // Checks the possibility of the
    // polygon exist or not
    if (possible(N, a, b, n))
 
        // Print nth angle
        // of the polygon
        cout << nth_angle(N, a, b, n);
    else
        cout << "Not Possible";
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if the angle
// is possible or not
static boolean possible(int N, int a,
                        int b, int n)
{
     
    // Angular sum of a N-sided polygon
    int sum_of_angle = 180 * (N - 2);
 
    // Angular sum of N-sided given polygon
    int Total_angle = (N * ((2 * a) +
                      (N - 1) * b)) / 2;
 
    // Check if both sum are equal
    if (sum_of_angle != Total_angle)
        return false;
    else
        return true;
}
 
// Function to find the nth angle
static int nth_angle(int N, int a,
                     int b, int n)
{
    int nth = 0;
 
    // Calculate nth angle
    nth = a + (n - 1) * b;
 
    // Return the nth angle
    return nth;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3, a = 30, b = 30, n = 3;
 
    // Checks the possibility of the
    // polygon exist or not
    if (possible(N, a, b, n))
         
        // Print nth angle
        // of the polygon
        System.out.print(nth_angle(N, a, b, n));
    else
        System.out.print("Not Possible");
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
 
# Function to check if the angle
# is possible or not
def possible(N, a, b, n):
     
    # Angular sum of a N-sided polygon
    sum_of_angle = 180 * (N - 2)
 
    # Angular sum of N-sided given polygon
    Total_angle = (N * ((2 * a) +
                  (N - 1) * b)) / 2
 
    # Check if both sum are equal
    if (sum_of_angle != Total_angle):
        return False
    else:
        return True
 
# Function to find the nth angle
def nth_angle(N, a, b, n):
    nth = 0
 
    # Calculate nth angle
    nth = a + (n - 1) * b
 
    # Return the nth angle
    return nth
 
# Driver Code
if __name__ == '__main__':
 
    N = 3
    a = 30
    b = 30
    n = 3
 
    # Checks the possibility of the
    # polygon exist or not
    if (possible(N, a, b, n)):
 
        # Print nth angle
        # of the polygon
        print(nth_angle(N, a, b, n))
    else:
        print("Not Possible")
 
# This code is contributed by Mohit Kumar


C#
// C# program for the above approach
using System;
class GFG{
  
// Function to check if the angle
// is possible or not
static bool possible(int N, int a,
                     int b, int n)
{
      
    // Angular sum of a N-sided polygon
    int sum_of_angle = 180 * (N - 2);
  
    // Angular sum of N-sided given polygon
    int Total_angle = (N * ((2 * a) +
                      (N - 1) * b)) / 2;
  
    // Check if both sum are equal
    if (sum_of_angle != Total_angle)
        return false;
    else
        return true;
}
  
// Function to find the nth angle
static int nth_angle(int N, int a,
                     int b, int n)
{
    int nth = 0;
  
    // Calculate nth angle
    nth = a + (n - 1) * b;
  
    // Return the nth angle
    return nth;
}
  
// Driver Code
public static void Main(string[] args)
{
    int N = 3, a = 30, b = 30, n = 3;
  
    // Checks the possibility of the
    // polygon exist or not
    if (possible(N, a, b, n))
          
        // Print nth angle
        // of the polygon
        Console.Write(nth_angle(N, a, b, n));
    else
        Console.Write("Not Possible");
}
}
  
// This code is contributed by Ritik Bansal


Javascript


输出:
90

时间复杂度: O(1)
辅助空间: O(1)