본문 바로가기

컴퓨터/백준 알고리즘

백준 알고리즘 - 10101번: 삼각형 외우기 [Java]

문제: https://www.acmicpc.net/problem/10101

 

10101번: 삼각형 외우기

문제의 설명에 따라 Equilateral, Isosceles, Scalene, Error 중 하나를 출력한다.

www.acmicpc.net

1. 코드

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] arr = new int[3];
		String answer = "Scalene";
		int sum = 0;
		for(int i = 0; i < 3; i++) {
			arr[i] = sc.nextInt();
			sum += arr[i];
			
			int cnt = 0;
			for(int j = 0; j < i; j++) {
				if(arr[j] == arr[i])
					cnt++;
			}
			if(cnt == 1)
				answer = "Isosceles";
			if(cnt == 2)
				answer = "Equilateral";
		}
		if(sum == 180)
			System.out.println(answer);
		else
			System.out.println("Error");
	}
}

2. 설명

반복문을 이용하여 삼각형 각도의 입력과 비교를 진행하여 겹치는 것이 있는 경우 answer의 값을 변환하며 180이라면 그대로 출력하는데 아니라면 Error를 출력합니다.