Dados dos enteros X e Y . La tarea es encontrar dos vértices de un triángulo isósceles ABC (ángulo recto en B) que tiene un vértice en el punto B (0, 0). Y hay un rectángulo con lados opuestos (0, 0) y (X, Y). Todos los puntos de este rectángulo están ubicados dentro o en el borde del triángulo. Imprime 4 enteros x1, y1, x2, y2, donde A(x1, y1) y B(x2, y2).
Ejemplos:
Entrada: X = 3, Y = 3
Salida: 6 0 0 6
Entrada: X = -3, y = -2
Salida: -5 0 0 -5
Enfoque:
Sea Val = |x| + |y|. Entonces el primer punto es (Val * sign(x), 0) y el segundo punto es (0, Val * sign(y)).
Veamos cómo funciona para x > 0 y y > 0. Otros casos se pueden demostrar de manera similar.
Necesitamos mostrar que (x, y) pertenece a nuestro triángulo (incluidos sus bordes). De hecho (x, y) pertenece al segmento, conectando (x + y, 0) con (0, x + y). La recta que pasa por (x + y, 0) y (0, x + y) es Y = – X + x + y. El uso de coordenadas (x, y) en esta ecuación prueba nuestra respuesta.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find two vertices of an // isosceles triangle in which there is // rectangle with opposite side (0, 0) and (x, y) #include <bits/stdc++.h> using namespace std; // Function to find two vertices of an // isosceles triangle in which there is // rectangle with opposite side (0, 0) and (x, y) int Vertices(int x, int y) { // Required value; int val = abs(x) + abs(y); // print x1 and y1 cout << val * (x < 0 ? -1 : 1) << " 0 "; // print x2 and y3 cout << "0 " << val * (y < 0 ? -1 : 1); } // Driver code int main() { int x = 3, y = 3; // Function call Vertices(x, y); return 0; }
Java
// Java program to find two vertices of an // isosceles triangle in which there is // rectangle with opposite side (0, 0) and (x, y) class GFG { // Function to find two vertices of an // isosceles triangle in which there is // rectangle with opposite side (0, 0) and (x, y) static void Vertices(int x, int y) { // Required value; int val = Math.abs(x) + Math.abs(y); // print x1 and y1 System.out.print(val * (x < 0 ? -1 : 1) + " 0 "); // print x2 and y3 System.out.print("0 " + val * (y < 0 ? -1 : 1)); } // Driver code public static void main(String[] args) { int x = 3, y = 3; // Function call Vertices(x, y); } } // This code is contributed by Rajput-Ji
Python3
# Python3 program to find two vertices of an # isosceles triangle in which there is # rectangle with opposite side (0, 0) and (x, y) # Function to find two vertices of an # isosceles triangle in which there is # rectangle with opposite side (0, 0) and (x, y) def Vertices(x, y) : # Required value; val = abs(x) + abs(y); # print x1 and y1 if x < 0 : x = -1 else : x = 1 print(val * x,"0",end = " "); # print x2 and y3 if y < 0 : y = -1 else : y = 1 print("0",val * y); # Driver code if __name__ == "__main__" : x = 3; y = 3; # Function call Vertices(x, y); # This code is contributed by AnkitRai01
C#
// C# program to find two vertices of an // isosceles triangle in which there is // rectangle with opposite side (0, 0) and (x, y) using System; class GFG { // Function to find two vertices of an // isosceles triangle in which there is // rectangle with opposite side (0, 0) and (x, y) static void Vertices(int x, int y) { // Required value; int val = Math.Abs(x) + Math.Abs(y); // print x1 and y1 Console.Write(val * (x < 0 ? -1 : 1) + " 0 "); // print x2 and y3 Console.Write("0 " + val * (y < 0 ? -1 : 1)); } // Driver code public static void Main(String[] args) { int x = 3, y = 3; // Function call Vertices(x, y); } } // This code is contributed by Rajput-Ji
Javascript
<script> // JavaScript program to find two vertices of an // isosceles triangle in which there is // rectangle with opposite side (0, 0) and (x, y) // Function to find two vertices of an // isosceles triangle in which there is // rectangle with opposite side (0, 0) and (x, y) function Vertices(x, y) { // Required value; let val = Math.abs(x) + Math.abs(y); // print x1 and y1 document.write(val * (x < 0 ? -1 : 1) + " 0 "); // print x2 and y3 document.write("0 " + val * (y < 0 ? -1 : 1)); } // Driver code let x = 3, y = 3; // Function call Vertices(x, y); // This code is contributed by Surbhi Tyagi. </script>
6 0 0 6
Complejidad de tiempo : O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA