给定三个整数a,b和c作为三元组。检查是否可以制作直角三角形。如果可能的话,否则没有打印是。 10 -18 <= a,b,c <= 10 18
例子:
Input: 3 4 5
Output: Yes
Explanation:
Since 3*3 + 4*4 = 5*5
Hence print "Yes"
Input: 8 5 13
Since 8 + 5 < 13 which violates the property of
triangle. Hence print "No"
为了使直角三角形有效,它必须满足以下条件:
- a,b和c应该大于0。
- 三角形的任意两个边的和必须大于第三个边。
- 勾股定理,即a 2 + b 2 = c 2 。
前两个条件很容易检查,但第三个条件我们必须注意溢出。由于a,b和c可能很大,因此除非我们在Java使用Python或BigInteger库,否则我们无法直接比较它们。对于C和C++之类的语言,我们必须减少分数形式的表达。
在比较分数之前,我们需要通过将它们的分子和分母除以gcd来将它们转换为简化形式。现在比较LHS和RHS的两个分数的分子和分母,以便如果两者都相同,则表示有效的直角三角形,否则表示有效的直角三角形。
C++
// C++ program to check validity of triplets
#include
using namespace std;
// Function to check pythagorean triplets
bool Triplets(long long a, long long b, long long c)
{
if (a <= 0 || b <= 0 || c <= 0)
return false;
vector vec{ a, b, c };
sort(vec.begin(), vec.end());
// Re-initialize a, b, c in ascending order
a = vec[0], b = vec[1], c = vec[2];
// Check validation of sides of triangle
if (a + b <= c)
return false;
long long p1 = a, p2 = c - b;
// Reduce fraction to simplified form
long long div = __gcd(p1, p2);
p1 /= div, p2 /= div;
long long q1 = c + b, q2 = a;
// Reduce fraction to simplified form
div = __gcd(q1, q2);
q1 /= div, q2 /= div;
// If fraction are equal return
// 'true' else 'false'
return (p1 == q1 && p2 == q2);
}
// Function that will return 'Yes' or 'No'
// according to the correction of triplets
string checkTriplet(long long a, long long b, long long c)
{
if (Triplets(a, b, c))
return "Yes";
else
return "No";
}
// Driver code
int main()
{
long long a = 4, b = 3, c = 5;
cout << checkTriplet(a, b, c) << endl;
a = 8, b = 13, c = 5;
cout << checkTriplet(a, b, c) << endl;
a = 1200000000000, b = 1600000000000,
c = 2000000000000;
cout << checkTriplet(a, b, c) << endl;
return 0;
}
Java
// Java program to check validity of triplets
import java.util.*;
class GFG
{
// Function to check pythagorean triplets
static boolean Triplets(long a,
long b, long c)
{
if (a <= 0 || b <= 0 || c <= 0)
return false;
long []vec = { a, b, c };
Arrays.sort(vec);
// Re-initialize a, b, c in ascending order
a = vec[0]; b = vec[1]; c = vec[2];
// Check validation of sides of triangle
if (a + b <= c)
return false;
long p1 = a, p2 = c - b;
// Reduce fraction to simplified form
long div = __gcd(p1, p2);
p1 /= div; p2 /= div;
long q1 = c + b, q2 = a;
// Reduce fraction to simplified form
div = __gcd(q1, q2);
q1 /= div; q2 /= div;
// If fraction are equal return
// 'true' else 'false'
return (p1 == q1 && p2 == q2);
}
// Function that will return 'Yes' or 'No'
// according to the correction of triplets
static String checkTriplet(long a,
long b, long c)
{
if (Triplets(a, b, c))
return "Yes";
else
return "No";
}
static long __gcd(long a, long b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver code
public static void main(String[] args)
{
long a = 4, b = 3, c = 5;
System.out.println(checkTriplet(a, b, c));
a = 8; b = 13; c = 5;
System.out.println(checkTriplet(a, b, c));
a = 1200000000000L; b = 1600000000000L;
c = 2000000000000L;
System.out.println(checkTriplet(a, b, c));
}
}
// This code is contributed
// by Princi Singh
Python3
# Python3 program to check validity of triplets
def Triplets(a, b, c):
if (a <= 0 or b <= 0 or c <= 0):
return False
vec = [ a, b, c ]
vec.sort()
# Re - initialize a, b, c in ascending order
a = vec[0]; b = vec[1]; c = vec[2]
# Check validation of sides of triangle
if (a + b <= c):
return False
p1 = a; p2 = c - b
# Reduce fraction to simplified form
div = __gcd(p1, p2)
p1 //= div
p2 //= div
q1 = c + b
q2 = a
# Reduce fraction to simplified form
div = __gcd(q1, q2)
q1 //= div
q2 //= div
# If fraction are equal return
# 'true' else 'false'
return (p1 == q1 and p2 == q2)
# Function that will return 'Yes' or 'No'
# according to the correction of triplets
def checkTriplet(a, b, c):
if (Triplets(a, b, c)):
return "Yes"
else:
return "No"
def __gcd(a, b):
if (b == 0):
return a
return __gcd(b, a % b)
# Driver code
a = 4
b = 3
c = 5
print(checkTriplet(a, b, c))
a = 8
b = 13
c = 5
print(checkTriplet(a, b, c))
a = 1200000000000
b = 1600000000000
c = 2000000000000
print(checkTriplet(a, b, c))
# This code is contributed by ng24_7
C#
// C# program to check validity of triplets
using System;
class GFG
{
// Function to check pythagorean triplets
static Boolean Triplets(long a,
long b, long c)
{
if (a <= 0 || b <= 0 || c <= 0)
return false;
long []vec = { a, b, c };
Array.Sort(vec);
// Re-initialize a, b, c in ascending order
a = vec[0]; b = vec[1]; c = vec[2];
// Check validation of sides of triangle
if (a + b <= c)
return false;
long p1 = a, p2 = c - b;
// Reduce fraction to simplified form
long div = __gcd(p1, p2);
p1 /= div; p2 /= div;
long q1 = c + b, q2 = a;
// Reduce fraction to simplified form
div = __gcd(q1, q2);
q1 /= div; q2 /= div;
// If fraction are equal return
// 'true' else 'false'
return (p1 == q1 && p2 == q2);
}
// Function that will return 'Yes' or 'No'
// according to the correction of triplets
static String checkTriplet(long a,
long b, long c)
{
if (Triplets(a, b, c))
return "Yes";
else
return "No";
}
static long __gcd(long a, long b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
// Driver code
public static void Main(String[] args)
{
long a = 4, b = 3, c = 5;
Console.WriteLine(checkTriplet(a, b, c));
a = 8; b = 13; c = 5;
Console.WriteLine(checkTriplet(a, b, c));
a = 1200000000000L; b = 1600000000000L;
c = 2000000000000L;
Console.WriteLine(checkTriplet(a, b, c));
}
}
// This code has been contributed by 29AjayKumar
输出:
Yes
No
Yes
时间复杂度: O(log(M)),其中M是a,b和c中的最大值。
辅助空间: O(1)