Dados dos números enteros r1 y r2 , que representan el radio de dos círculos, la tarea es encontrar el radio del círculo que tiene un área igual a la suma del área de los dos círculos que tienen radios dados.
Ejemplos:
Entrada:
r1 = 8
r2 = 6
Salida:
10
Explicación:
Área de círculo con radio 8 = 201.061929
Área de círculo con radio 6 = 113.097335
Área de círculo con radio 10 = 314.159265Entrada:
r1 = 2
r2 = 2
Salida:
2,82843
Enfoque: siga los pasos a continuación para resolver el problema:
- Calcular el área del primer círculo es a1 = 3.14 * r1 * r1 .
- Calcular el área del segundo círculo es a2 = 3.14 * r2 * r2 .
- Por lo tanto, el área del tercer círculo es a1 + a2 .
- El radio del tercer círculo es sqrt (a3 / 3.14)
A continuación se muestra la implementación del siguiente enfoque.
C++14
// C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate radius of the circle // having area equal to sum of the area of // two circles with given radii double findRadius(double r1, double r2) { double a1, a2, a3, r3; // Area of first circle a1 = 3.14 * r1 * r1; // Area of second circle a2 = 3.14 * r2 * r2; // Area of third circle a3 = a1 + a2; // Radius of third circle r3 = sqrt(a3 / 3.14); return r3; } // Driver Code int main() { // Given radius double r1 = 8, r2 = 6; // Prints the radius of // the required circle cout << findRadius(r1, r2); return 0; }
Java
// Java program to implement // the above approach import java.util.*; class GFG { // Function to calculate radius of the circle // having area equal to sum of the area of // two circles with given radii static double findRadius(double r1, double r2) { double a1, a2, a3, r3; // Area of first circle a1 = 3.14 * r1 * r1; // Area of second circle a2 = 3.14 * r2 * r2; // Area of third circle a3 = a1 + a2; // Radius of third circle r3 = Math.sqrt(a3 / 3.14); return r3; } // Driver code public static void main(String[] args) { // Given radius double r1 = 8, r2 = 6; // Prints the radius of // the required circle System.out.println((int)findRadius(r1, r2)); } } // This code is contributed by code_hunt.
Python3
# Python program to implement # the above approach # Function to calculate radius of the circle # having area equal to sum of the area of # two circles with given radii def findRadius(r1, r2): a1, a2, a3, r3 = 0, 0, 0, 0; # Area of first circle a1 = 3.14 * r1 * r1; # Area of second circle a2 = 3.14 * r2 * r2; # Area of third circle a3 = a1 + a2; # Radius of third circle r3 = ((a3 / 3.14)**(1/2)); return r3; # Driver code if __name__ == '__main__': # Given radius r1 = 8; r2 = 6; # Prints the radius of # the required circle print(int(findRadius(r1, r2))); # This code is contributed by 29AjayKumar
C#
// C# program to implement // the above approach using System; class GFG { // Function to calculate radius of the circle // having area equal to sum of the area of // two circles with given radii static double findRadius(double r1, double r2) { double a1, a2, a3, r3; // Area of first circle a1 = 3.14 * r1 * r1; // Area of second circle a2 = 3.14 * r2 * r2; // Area of third circle a3 = a1 + a2; // Radius of third circle r3 = Math.Sqrt(a3 / 3.14); return r3; } // Driver code static void Main() { // Given radius double r1 = 8, r2 = 6; // Prints the radius of // the required circle Console.WriteLine((int)findRadius(r1, r2)); } } // This code is contributed by susmitakundugoaldanga
Javascript
<script> // Javascript program of the above approach // Function to calculate radius of the circle // having area equal to sum of the area of // two circles with given radii function findRadius(r1, r2) { let a1, a2, a3, r3; // Area of first circle a1 = 3.14 * r1 * r1; // Area of second circle a2 = 3.14 * r2 * r2; // Area of third circle a3 = a1 + a2; // Radius of third circle r3 = Math.sqrt(a3 / 3.14); return r3; } // Driver Code // Given radius let r1 = 8, r2 = 6; // Prints the radius of // the required circle document.write(findRadius(r1, r2)); </script>
10
Complejidad de tiempo : O (sqrt (n)) desde que se usa la función sqrt incorporada
Complejidad de espacio: O (1)
Publicación traducida automáticamente
Artículo escrito por abhishekjha3035 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA