Dados cuatro enteros N, A, K, n donde N representa el número de lados del polígono, A representa el ángulo inicial del polígono, K representa el aumento por ángulo, la tarea es encontrar el ángulo n del polígono que tiene N lados _ Si no es posible, imprima -1 .
Ejemplos:
Entrada: N = 3, A = 30, K = 30, n = 3
Salida: 90
Explicación:
El tercer ángulo del polígono que tiene tres lados es de 90 grados ya que todos los ángulos son 30, 60, 90.Entrada: N = 4, A = 40, K = 30, n = 3
Salida: -1
Explicación:
No es posible crear ese polígono cuyo ángulo inicial es 40 y no. de lados son 4.
Enfoque: La idea es observar que los ángulos del polígono están aumentando en los términos de Progresión Aritmética por una diferencia de K grado.
- Encuentra la suma angular del polígono de N lados y la suma de los ángulos del polígono dado .
- Compruebe si ambos valores son iguales o no. En caso afirmativo, entonces el ángulo n es posible, por lo tanto, encuentre el ángulo n .
- De lo contrario, imprima -1 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <iostream> 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
<script> // JavaScript program for the above approach // Function to check if the angle // is possible or not function possible(N, a, b, n) { // Angular sum of a N-sided polygon let sum_of_angle = 180 * (N - 2); // Angular sum of N-sided given polygon let Total_angle = Math.floor((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 function nth_angle(N, a, b, n) { let nth = 0; // Calculate nth angle nth = a + (n - 1) * b; // Return the nth angle return nth; } // Driver Code let 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 document.write(nth_angle(N, a, b, n)); else document.write("Not Possible"); // This code is contributed by Surbhi Tyagi. </script>
90
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por thakurabhaysingh445 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA