给定两个代表三角形边的正整数A和B ,任务是检查三角形的给定两个边是否是有效直角三角形的边。如果发现是真的,则打印“是”。否则,打印“否” 。
例子:
Input: A = 3, B = 4
Output: Yes
Explanation: A right-angled triangle is possible with side lengths 3, 4 and 5.
Input : A = 2, B = 5
Output: No
方法:请按照以下步骤解决问题:
- 初始化变量,使用checkTriangle来检查三角形的给定两个边是否不是直角三角形的边。
- 检查B 2 + A 2的值是否是完美的平方。如果发现为真,则更新checkTriangle = True。 。
- 否则,请检查B 2 – A 2的值是否为理想平方。如果发现为真,则更新checkTriangle = True。
- 否则,请检查A 2 – B 2的值是否为正整数。如果发现为真,则更新checkTriangle = True。
- 最后,如果checkTriangle为True ,则打印“是” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to check if N is a
// perfect square number or not
int checkPerfectSquare(int N)
{
// If N is a non
// positive integer
if (N <= 0) {
return 0;
}
// Stores square root
// of N
double sq = sqrt(N);
// Check for perfect square
if (floor(sq) == ceil(sq)) {
return 1;
}
// If N is not a
// perfect square number
return 0;
}
// Function to check if given two sides of a
// triangle forms a right-angled triangle
bool checktwoSidesareRighTriangle(int A, int B)
{
bool checkTriangle = false;
// If the value of (A * A + B * B) is a
// perfect square number
if (checkPerfectSquare(A * A + B * B)) {
// Update checkTriangle
checkTriangle = true;
}
// If the value of (A * A - B * B) is a
// perfect square number
if (checkPerfectSquare(A * A - B * B)) {
// Update checkTriangle
checkTriangle = true;
}
// If the value of (B * B - A * A) is a
// perfect square number
if (checkPerfectSquare(B * B - A * A)) {
// Update checkTriangle
checkTriangle = true;
}
return checkTriangle;
}
// Driver Code
int main()
{
int A = 3, B = 4;
// If the given two sides of a triangle
// forms a right-angled triangle
if (checktwoSidesareRighTriangle(A, B)) {
cout << "Yes";
}
// Otherwise
else {
cout << "No";
}
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to check if N is a
// perfect square number or not
static int checkPerfectSquare(int N)
{
// If N is a non
// positive integer
if (N <= 0)
{
return 0;
}
// Stores square root
// of N
double sq = Math.sqrt(N);
// Check for perfect square
if (Math.floor(sq) == Math.ceil(sq))
{
return 1;
}
// If N is not a
// perfect square number
return 0;
}
// Function to check if given two sides of a
// triangle forms a right-angled triangle
static boolean checktwoSidesareRighTriangle(int A,
int B)
{
boolean checkTriangle = false;
// If the value of (A * A + B * B) is a
// perfect square number
if (checkPerfectSquare(A * A + B * B) != 0)
{
// Update checkTriangle
checkTriangle = true;
}
// If the value of (A * A - B * B) is a
// perfect square number
if (checkPerfectSquare(A * A - B * B) != 0)
{
// Update checkTriangle
checkTriangle = true;
}
// If the value of (B * B - A * A) is a
// perfect square number
if (checkPerfectSquare(B * B - A * A) != 0)
{
// Update checkTriangle
checkTriangle = true;
}
return checkTriangle;
}
// Driver Code
public static void main(String[] args)
{
int A = 3, B = 4;
// If the given two sides of a triangle
// forms a right-angled triangle
if (checktwoSidesareRighTriangle(A, B))
{
System.out.print("Yes");
}
// Otherwise
else
{
System.out.print("No");
}
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program to implement
# the above approach
from math import sqrt, floor, ceil
# Function to check if N is a
# perfect square number or not
def checkPerfectSquare(N):
# If N is a non
# positive integer
if (N <= 0):
return 0
# Stores square root
# of N
sq = sqrt(N)
# Check for perfect square
if (floor(sq) == ceil(sq)):
return 1
# If N is not a
# perfect square number
return 0
# Function to check if given two sides of a
# triangle forms a right-angled triangle
def checktwoSidesareRighTriangle(A, B):
checkTriangle = False
# If the value of (A * A + B * B) is a
# perfect square number
if (checkPerfectSquare(A * A + B * B)):
# Update checkTriangle
checkTriangle = True
# If the value of (A * A - B * B) is a
# perfect square number
if (checkPerfectSquare(A * A - B * B)):
# Update checkTriangle
checkTriangle = True
# If the value of (B * B - A * A) is a
# perfect square number
if (checkPerfectSquare(B * B - A * A)):
# Update checkTriangle
checkTriangle = True
return checkTriangle
# Driver Code
if __name__ == '__main__':
A = 3
B = 4
# If the given two sides of a triangle
# forms a right-angled triangle
if (checktwoSidesareRighTriangle(A, B)):
print("Yes")
# Otherwise
else:
print("No")
# This code is contributed by SURENDRA_GANGWAR
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to check if N is a
// perfect square number or not
static int checkPerfectSquare(int N)
{
// If N is a non
// positive integer
if (N <= 0)
{
return 0;
}
// Stores square root
// of N
double sq = Math.Sqrt(N);
// Check for perfect square
if (Math.Floor(sq) == Math.Ceiling(sq))
{
return 1;
}
// If N is not a
// perfect square number
return 0;
}
// Function to check if given two sides of a
// triangle forms a right-angled triangle
static bool checktwoSidesareRighTriangle(int A,
int B)
{
bool checkTriangle = false;
// If the value of (A * A + B * B) is a
// perfect square number
if (checkPerfectSquare(A * A + B * B) != 0)
{
// Update checkTriangle
checkTriangle = true;
}
// If the value of (A * A - B * B) is a
// perfect square number
if (checkPerfectSquare(A * A - B * B) != 0)
{
// Update checkTriangle
checkTriangle = true;
}
// If the value of (B * B - A * A) is a
// perfect square number
if (checkPerfectSquare(B * B - A * A) != 0)
{
// Update checkTriangle
checkTriangle = true;
}
return checkTriangle;
}
// Driver Code
public static void Main()
{
int A = 3, B = 4;
// If the given two sides of a triangle
// forms a right-angled triangle
if (checktwoSidesareRighTriangle(A, B))
{
Console.Write("Yes");
}
// Otherwise
else
{
Console.Write("No");
}
}
}
// This code is contributed by code_hunt
Javascript
输出:
Yes
时间复杂度: O(log(max(A,B))
辅助空间: O(1)