Dado un número entero N , la tarea es encontrar dos números a y b tales que a/b = N y a – b = N. Escriba “No” si no son posibles tales números.
Ejemplos:
Entrada: N = 6
Salida:
a = 7.2
b = 1.2
Explicación:
Para los dos números dados a y b, a/b = 6 = N y ab = 6 = NEntrada: N = 1
Salida: No
Explicación:
No hay valores de a y b que satisfagan la condición.
Planteamiento: Para resolver el problema observe las ecuaciones derivadas a continuación:
Al resolver las ecuaciones anteriores simultáneamente, obtenemos:
Dado que el denominador es N – 1 , la respuesta no será posible cuando N = 1 . Para todos los demás casos, la respuesta es posible. Por lo tanto, encuentre los valores de a y b respectivamente.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find two numbers with // difference and division both as N void findAandB(double N) { // Condition if the answer // is not possible if (N == 1) { cout << "No"; return; } // Calculate a and b double a = N * N / (N - 1); double b = a / N; // Print the values cout << "a = " << a << endl; cout << "b = " << b << endl; } // Driver Code int main() { // Given N double N = 6; // Function Call findAandB(N); return 0; }
Java
// Java program for the above approach class GFG{ // Function to find two numbers with // difference and division both as N static void findAandB(double N) { // Condition if the answer // is not possible if (N == 1) { System.out.print("No"); return; } // Calculate a and b double a = N * N / (N - 1); double b = a / N; // Print the values System.out.print("a = " + a + "\n"); System.out.print("b = " + b + "\n"); } // Driver Code public static void main(String[] args) { // Given N double N = 6; // Function call findAandB(N); } } // This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach # Function to find two numbers with # difference and division both as N def findAandB(N): # Condition if the answer # is not possible if (N == 1): print("No") return # Calculate a and b a = N * N / (N - 1) b = a / N # Print the values print("a = ", a) print("b = ", b) # Driver Code # Given N N = 6 # Function call findAandB(N) # This code is contributed by sanjoy_62
C#
// C# program for the above approach using System; class GFG{ // Function to find two numbers with // difference and division both as N static void findAandB(double N) { // Condition if the answer // is not possible if (N == 1) { Console.Write("No"); return; } // Calculate a and b double a = N * N / (N - 1); double b = a / N; // Print the values Console.Write("a = " + a + "\n"); Console.Write("b = " + b + "\n"); } // Driver Code public static void Main(String[] args) { // Given N double N = 6; // Function call findAandB(N); } } // This code is contributed by amal kumar choubey
Javascript
<script> // Javascript program for the above approach // Function to find two numbers with // difference and division both as N function findAandB( N) { // Condition if the answer // is not possible if (N == 1) { document.write("No"); return; } // Calculate a and b let a = N * N / (N - 1); let b = a / N; // Print the values document.write("a = " + a + "<br/>"); document.write("b = " + b + "<br/>"); } // Driver Code // Given N let N = 6; // Function call findAandB(N); // This code contributed by aashish1995 </script>
a = 7.2 b = 1.2
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)