
난이도: ★☆☆☆☆ • solved on: 2025-12-14

0 0 0이 입력되면 종료해야 한다.자료구조
int[] : 세 변을 배열로 관리알고리즘/기법
핵심 키워드
a + b > c (정렬 후)
- 문제 분해
- 입력을 한 줄씩 읽고
0 0 0이면 종료- 3개의 값을 비교/스왑해서 오름차순 비슷하게 정리
- 삼각형 성립 여부 판단 → 종류 출력
핵심 로직 흐름
while not (0 0 0): swap 비교로 3개를 정렬에 가깝게 만들기 min, middle, max로 분리 if (min + middle <= max) -> Invalid else if (모두 같음) -> Equilateral else if (둘만 같음) -> Isosceles else -> Scalene예외 처리
- 종료 조건
0 0 0
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] lines = sc.nextLine().split(" ");
while(!(lines[0].equals("0")&&lines[1].equals("0")&&lines[2].equals("0"))) {
if(Integer.valueOf(lines[0]).compareTo(Integer.valueOf(lines[1])) > 0) {
String tmp = lines[0];
lines[0] = lines[1];
lines[1] = tmp;
}
if(Integer.valueOf(lines[1]).compareTo(Integer.valueOf(lines[2])) > 0) {
String tmp = lines[1];
lines[1] = lines[2];
lines[2] = tmp;
}
if(Integer.valueOf(lines[0]).compareTo(Integer.valueOf(lines[2])) > 0) {
String tmp = lines[0];
lines[0] = lines[2];
lines[2] = tmp;
}
int min = Integer.valueOf(lines[0]);
int middle = Integer.valueOf(lines[1]);
int max = Integer.valueOf(lines[2]);
if(min + middle <= max || max < middle - min) {
System.out.println("Invalid");
} else {
if(min==middle&&max==middle) {
System.out.println("Equilateral");
} else if(min==middle||max==middle||min==max){
System.out.println("Isosceles");
} else {
System.out.println("Scalene");
}
}
lines = sc.nextLine().split(" ");
}
}
}
- 정렬을 직접 구현하지 않고 라이브러리로 처리
int[] sides = {a,b,c}; Arrays.sort(sides);- 정렬 후
sides[0] + sides[1] > sides[2]만 보면 됨.
입력 안정성/속도 개선
Scanner대신BufferedReader+StringTokenizer- 한 줄씩 읽다가
0 0 0이면break불필요한 조건 제거
- 정렬을 하면
max < middle - min같은 조건은 의미가 없어집니다. (이미min ≤ middle ≤ max)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String line = br.readLine();
if (line == null) break;
StringTokenizer st = new StringTokenizer(line);
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
if (a == 0 && b == 0 && c == 0) break;
int[] sides = {a, b, c};
Arrays.sort(sides); // sides[0] <= sides[1] <= sides[2]
int x = sides[0], y = sides[1], z = sides[2];
if (x + y <= z) {
System.out.println("Invalid");
} else if (x == y && y == z) {
System.out.println("Equilateral");
} else if (x == y || y == z) {
System.out.println("Isosceles");
} else {
System.out.println("Scalene");
}
}
}
}
방법 1
방법 2
a + b > c 한 줄로 정리되어 조건이 깔끔해진다.BufferedReader가 예외 상황(EOF 등)에도 더 안정적이다.비슷한 유형 (GPT 추천):
확장 문제 (GPT 추천):