给定三个整数作为X 、 Y和Z代表三角形的三个边,任务是检查由给定边形成的三角形是等边、等腰还是不等边形。
Equilateral Triangle: A triangle is said to be equilateral triangle if all the sides are equal. If X, Y, Z are three sides of the triangle. Then, the triangle is equilateral only if X = Y = Z.
Isosceles Triangle: A triangle is said to be an isosceles triangle if any of its two sides are equal. If X, Y, Z are three sides of the triangle.Then, the triangle is isosceles if either X = Y or X = Z or Y = Z.
Scalene Triangle: A triangle is said Scalene Triangle if none of its sides is equal.
例子:
Input: X = 6, Y = 8, Z = 10
Output: Scalene Triangle
Explanation:
Since all the sides of the given triangle are unequal, the triangle is scalene.
Input: X = 10, Y = 10, Z = 10
Output: Equilateral Triangle
Explanation:
Since all the sides of the given triangle are equal.
处理方法:按照以下步骤解决问题:
- 检查是否X = Y和Y = Z 。如果发现是真的,打印“等边三角形”。
- 如果它不是等边三角形,则检查是否X = Y或X = Z或Y = Z 。如果发现是真的,打印“等腰三角形”。
- 如果以上步骤都不满足,则打印“Scalene Triangle”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the triangle
// is equilateral or isosceles or scalene
void checkTriangle(int x, int y, int z)
{
// Check for equilateral triangle
if (x == y && y == z)
cout << "Equilateral Triangle";
// Check for isoceles triangle
else if (x == y || y == z || z == x)
cout << "Isoceles Triangle";
// Otherwise scalene triangle
else
cout << "Scalene Triangle";
}
// Driver Code
int main()
{
// Given sides of triangle
int x = 8, y = 7, z = 9;
// Function call
checkTriangle(x, y, z);
}
// This code is contributed by jana_sayantan
Java
// Java program for the above approach
class GFG{
// Function to check if the triangle
// is equilateral or isosceles or scalene
static void checkTriangle(int x, int y, int z)
{
// Check for equilateral triangle
if (x == y && y == z )
System.out.println("Equilateral Triangle");
// Check for isoceles triangle
else if (x == y || y == z || z == x )
System.out.println("Isoceles Triangle");
// Otherwise scalene triangle
else
System.out.println("Scalene Triangle");
}
// Driver Code
public static void main(String[] args)
{
// Given sides of triangle
int x = 8, y = 7, z = 9;
// Function call
checkTriangle(x, y, z);
}
}
// This code is contributed by jana_sayantan
Python3
# Python3 program for the above approach
# Function to check if the triangle
# is equilateral or isosceles or scalene
def checkTriangle(x, y, z):
# _Check for equilateral triangle
if x == y == z:
print("Equilateral Triangle")
# Check for isoceles triangle
elif x == y or y == z or z == x:
print("Isoceles Triangle")
# Otherwise scalene triangle
else:
print("Scalene Triangle")
# Driver Code
# Given sides of triangle
x = 8
y = 7
z = 9
# Function Call
checkTriangle(x, y, z)
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if the triangle
// is equilateral or isosceles or scalene
static void checkTriangle(int x, int y, int z)
{
// Check for equilateral triangle
if (x == y && y == z )
Console.WriteLine("Equilateral Triangle");
// Check for isoceles triangle
else if (x == y || y == z || z == x )
Console.WriteLine("Isoceles Triangle");
// Otherwise scalene triangle
else
Console.WriteLine("Scalene Triangle");
}
// Driver Code
public static void Main()
{
// Given sides of triangle
int x = 8, y = 7, z = 9;
// Function call
checkTriangle(x, y, z);
}
}
// This code is contributed by code_hunt
Javascript
Scalene Triangle
时间复杂度: O(1)
辅助空间: O(1)