给定直角三角形的一侧,请检查是否存在与三角形的任何其他两个侧面都可能存在的直角三角形。如果可能,请打印另两面的长度。
其他打印-1
注意:三角形的所有边都必须为正整数
范例1:
Input : a = 3
Output : b = 4, c = 5
Explanation : a = 3, b = 4 and c = 5 form right
angle triangle because
32 + 42 = 9 + 16 = 25 = 52 => 32 + 42 = 52
范例2:
Input : a = 11
Output : b = 60, c = 61
Explanation : a = 11, b = 60 and c = 61 form
right angle triangle because
112 + 602 = 121 + 3600 = 3721 = 612 => 112 + 602 = 612
为了解决这个问题,我们首先观察毕达哥拉斯方程。如果a和b是直角三角形的边的长度,而c是斜边的长度,则边的长度的平方之和等于斜边的长度的平方。
此关系由以下公式表示:
a2 + b2 = c2
- 情况1 – a是一个奇数:给定a,找到b和c
c2 - b2 = a2
OR
c = (a2 + 1)/2;
b = (a2 - 1)/2;
- 上述解决方案仅在a为奇数的情况下有效,因为2 + 1仅对奇数a可被2整除。
- 情况2 – a是偶数:当cb为2&c + b为(a 2 )/ 2
c-b = 2 & c+b = (a2)/2
Hence,
c = (a2)/4 + 1;
b = (a2)/4 - 1;
- 当a为偶数时有效。
该解决方案不适用于a = 1和a = 2的情况,因为在边1或2的所有整数边上不存在任何直角三角形。
C++
// C++ program to print other two sides of right
// angle traingle given one side
#include
using namespace std;
// Finds two sides of a right angle triangle
// if it exist.
void printOtherSides(int n)
{
// if n is odd
if (n & 1)
{
// case of n = 1 handled separately
if (n == 1)
cout << -1 << endl;
else
{
int b = (n*n-1)/2;
int c = (n*n+1)/2;
cout << "b = " << b
<< ", c = " << c << endl;
}
}
else
{
// case of n = 2 handled separately
if (n == 2)
cout << -1 << endl;
else
{
int b = n*n/4-1;
int c = n*n/4+1;
cout << "b = " << b
<< ", c = " << c << endl;
}
}
}
// Driver program to test above function
int main()
{
int a = 3;
printOtherSides(a);
return 0;
}
Java
// Java program to print other two
// sides of right angle traingle
// given one side
class GFG
{
// Finds two sides of a right angle
// triangle if it they exist.
static void printOtherSides(int n)
{
// if n is odd
if (n % 2 != 0)
{
// case of n = 1 handled separately
if (n == 1)
System.out.println("-1");
else
{
int b = (n * n -1) / 2;
int c = (n *n +1) / 2;
System.out.println("b = "+ b +
", c = "+c);
}
}
else
{
// case of n = 2 handled separately
if (n == 2)
System.out.println("-1");
else
{
int b = n * n / 4 - 1;
int c = n * n / 4 + 1;
System.out.println("b = "+ b +
", c = "+c);
}
}
}
// Driver code
public static void main (String[] args)
{
int a = 3;
printOtherSides(a);
}
}
// This code is contributed by Anant Agarwal.
Python
# Python program to print other
# two sides of right angle
# triangle given one side
# Finds two sides of a right angle
# triangle if it they exist.
def printOtherSides(n):
# if n is odd
if(n & 1):
# case of n = 1 handled
# separately
if(n == 1):
print(-1)
else:
b = (n * n - 1) // 2
c = (n * n + 1) // 2
print("b =", b, ", c =", c)
else:
# case of n = 2 handled
# separately
if(n == 2):
print(-1)
else:
b = n * n // 4 - 1
c = n * n // 4 + 1
print("b =", b", c =", c)
# Driver Code
a = 3
printOtherSides(a)
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to print other two
// sides of right angle traingle
// given one side
using System;
class GFG {
// Finds two sides of a right angle
// triangle if it they exist.
static void printOtherSides(int n)
{
// if n is odd
if (n % 2 != 0)
{
// case of n = 1 handled
// separately
if (n == 1)
Console.WriteLine("-1");
else
{
int b = (n * n - 1) / 2;
int c = (n * n + 1) / 2;
Console.Write("b = "+ b +
", c = "+ c);
}
}
else
{
// case of n = 2 handled
// separately
if (n == 2)
Console.Write("-1");
else
{
int b = n * n / 4 - 1;
int c = n * n / 4 + 1;
Console.Write("b = "+ b +
", c = "+ c);
}
}
}
// Driver code
public static void Main ()
{
int a = 3;
printOtherSides(a);
}
}
// This code is contributed by Nitin Mittal.
PHP
Javascript
输出:
b = 4, c = 5