https://www.acmicpc.net/problem/5073
์ฝ๋
import java.util.Scanner;
public class Main {
// 1)
static int maxNum(int a, int b, int c) {
int max = 0;
if(a > b) {
if(b > c)
max = a;
else
max = (a>c) ? a : c;
}
else
max = (b>c) ? b : c;
return max;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(true) {
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
String result = "";
if(a == 0 && b == 0 && c == 0)
break;
int max = maxNum(a, b, c); // a, b, c ์ค ์ต๋๊ฐ ์ฐพ๊ธฐ
// 2)
if(a == max) {
if(a >= b+c)
result = "Invalid";
}
else if(b == max) {
if(b >= a+c)
result = "Invalid";
}
else {
if(c >= a+b)
result = "Invalid";
}
// 3)
if(result == "" && a == b && b == c )
result = "Equilateral";
else if(result == "" && (a == b && b != c) || (a == c && b != c) || (b == c && a != b))
result = "Isosceles";
else if(result == "" && a != b && b != c && a != c)
result = "Scalene";
System.out.println(result);
}
scan.close();
}
}
ํ์ด
// 1)
๋ฌธ์ ์์ ์ฃผ์ด์ง ์กฐ๊ฑด์ ์ ๋ถ๋ค ๊ตฌํด์ ํ์๋ค.
๋จผ์ ์ธ ๋ณ์ ๊ธธ์ด์ค ์ต๋๊ฐ์ ์ฐพ๊ธฐ์ํด maxNum() ๋ฉ์๋๋ฅผ ๋ง๋ค์๋ค.
// ์ธ ์ซ์์ค ์ต๋๊ฐ ์ฐพ๊ธฐ
static int maxNum(int a, int b, int c) {
int max = 0;
if(a > b) {
if(b > c)
max = a;
else
max = (a>c) ? a : c;
}
else
max = (b>c) ? b : c;
return max;
}
a์ b๋ฅผ ๋น๊ตํ๊ณ , b์ c๋ฅผ ๋น๊ตํ๋ ์์ผ๋ก ์ต๋๊ฐ์ ๊ตฌํ๋ค.
// 2)
์ต๋๊ฐ์ ์ฐพ์ ํ ์ธ ๋ณ์ ๊ธธ์ด๊ฐ ์ผ๊ฐํ์ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ๋ฅผ ๊ตฌํด์ฃผ์๋ค.
(๊ฐ์ฅ ๊ธด๋ณ < ๋๋จธ์ง ๋ ๋ณ์ ๊ธธ์ด์ ํฉ)
// 2)
if(a == max) {
if(a >= b+c)
result = "Invalid";
}
else if(b == max) {
if(b >= a+c)
result = "Invalid";
}
else {
if(c >= a+b)
result = "Invalid";
}
// 3)
๋ง์ฝ ์ธ ๋ณ์ ๊ธธ์ด๊ฐ ์ผ๊ฐํ์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ค๋ฉด, result ๋ณ์๋ "" ๊ฐ ์ด๊ธฐ๊ฐ์ด๋ฏ๋ก ์กฐ๊ฑด์ ๋ฃ์ด์ค๋ค.
result๊ฐ ""์ด๋ฉด์ ์ธ ๋ณ์ ๊ธธ์ด๊ฐ ๊ฐ์ ๊ฒฝ์ฐ => "Equilateral"
result๊ฐ ""์ด๋ฉด์ ๋ ๋ณ์ ๊ธธ์ด๊ฐ ๊ฐ์ ๊ฒฝ์ฐ => "Isosceles"
-> ๋ ๋ณ์ ๊ธธ์ด๊ฐ ๊ฐ๊ณ , ๋ค๋ฅธ ํ ๋ณ์ ๊ธธ์ด์๋ ๋ฌ๋ผ์ผ ํ๋ค.
result๊ฐ ""์ด๋ฉด์ ์ธ ๋ณ์ ๊ธธ์ด๊ฐ ๋ชจ๋ ๋ค๋ฅธ๊ฒฝ์ฐ => "Scalene"
if(result == "" && a == b && b == c )
result = "Equilateral";
else if(result == "" && (a == b && b != c) || (a == c && b != c) || (b == c && a != b))
result = "Isosceles";
else if(result == "" && a != b && b != c && a != c)
result = "Scalene";
๊ทธ ํ result๋ฅผ ์ถ๋ ฅํด์ฃผ๋ฉด ๋.
๋ค๋ฅธ ํ์ด
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
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;
if ((a >= b + c) || (b >= a + c) || (c >= a + b))
System.out.println("Invalid");
else if (a == b && b == c)
System.out.println("Equilateral");
else if ((a == b) && (a != c) || (a == c) && (a != b) || (b == c) && (b != a))
System.out.println("Isosceles");
else if ((a != b) && (b != c) && (a != c))
System.out.println("Scalene");
else
System.out.println("Invalid");
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int a, b, c;
while(true)
{
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
if(a == 0 && b == 0 && c == 0)
break;
if(a == b && b == c && c == a)
System.out.println("Equilateral");
else if(a + b <= c || a + c <= b || b + c <= a)
System.out.println("Invalid");
else if(a == b || b == c || c == a)
System.out.println("Isosceles");
else
System.out.println("Scalene");
}
}
}
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 14697๋ฒ: ๋ฐฉ ๋ฐฐ์ ํ๊ธฐ (0) | 2020.01.17 |
---|---|
[๋ฐฑ์ค] 15953๋ฒ: ์๊ธ ํํฐ (0) | 2020.01.17 |
[๋ฐฑ์ค] 2446๋ฒ: ๋ณ ์ฐ๊ธฐ - 9 (0) | 2020.01.16 |
[๋ฐฑ์ค] 2445๋ฒ: ๋ณ ์ฐ๊ธฐ - 8 (0) | 2020.01.16 |
[๋ฐฑ์ค] 10820๋ฒ: ๋ฌธ์์ด ๋ถ์ (0) | 2020.01.16 |
๋๊ธ