给定两个整数X和Y。任务是找到等腰三角形ABC(在B处为直角)的两个顶点,该顶点在点B(0,0)处具有一个顶点。并且有一个具有相反边(0,0)和(X,Y)的矩形。该矩形的所有点都位于三角形的内部或边界上。打印4个整数x1,y1,x2,y2,其中A(x1,y1)和B(x2,y2)。
例子:
Input : X = 3, Y = 3
Output : 6 0 0 6
Input : X = -3, y = -2
Output : -5 0 0 -5
方法 :
令Val = | x | + | y |。然后第一个点是(Val * sign(x),0),第二个点是(0,Val * sign(y))。
让我们看看它在x> 0和y> 0时如何工作。其他情况也可以用类似的方式证明。
我们需要证明(x,y)属于我们的三角形(包括其边界)。实际上(x,y)属于段,将(x + y,0)与(0,x + y)连接起来。通过(x + y,0)和(0,x + y)的线是Y = – X + x + y。在此等式中使用坐标(x,y)证明了我们的答案。
下面是上述方法的实现:
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
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
输出:
6 0 0 6
时间复杂度: O(1)