905. What type of triangle?

 

Determine the type of a triangle (equilateral, isosceles, or scalene) based on the given side lengths.

 

Input. One line contains 3 integers, representing the lengths of the triangle’s sides. The side lengths do not exceed 100.

 

Output. Print 1 if the triangle is equilateral, 2 if it is isosceles, and 3 if it is scalene.

 

Sample input

Sample output

3 4 3

2

 

 

SOLUTION

conditional statement

 

Algorithm analysis

A triangle is equilateral if all its sides are equal. If it is not equilateral, check if it is isosceles – this requires two sides to be equal. If the triangle is neither equilateral nor isosceles, then it is scalene.

 

Algorithm implementation

Read the input data.

 

scanf("%d %d %d",&a,&b,&c);

 

Check if the triangle is equilateral.

 

if ((a == b) && (b == c))

  puts("1");

else

 

Check if the triangle is isosceles.

 

if ((a == b ) || (a == c) || (b == c))

  puts("2");

else

 

Otherwise, the triangle is scalene.

 

  puts("3");

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int a = con.nextInt();

    int b = con.nextInt();

    int c = con.nextInt();

   

    if ((a == b) && (b == c))

      System.out.println("1");

    else

    if ((a == b ) || (a == c) || (b == c))

      System.out.println("2");

    else

      System.out.println("3");

   

    con.close();

  }

}   

 

Python implementation

Read the input data.

 

a, b, c = map(int, input().split())

 

Check if the triangle is equilateral.

 

if a == b and b == c:

  print("1")

else:

 

Check if the triangle is isosceles.

 

  if a == b or a == c or b == c:

    print("2")

  else:

 

Otherwise, the triangle is scalene.

 

    print("3")