코딩테스트/JAVA
[ 백준 ] 2480번 : 주사위 세개 _ JAVA
어서오시우
2023. 11. 27. 16:52
728x90
문제
내 풀이
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));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int dice1 = Integer.parseInt(st.nextToken());
int dice2 = Integer.parseInt(st.nextToken());
int dice3 = Integer.parseInt(st.nextToken());
if ((dice1 == dice2)) {
if (dice1 == dice3) {
System.out.println(10000 + (dice1 * 1000));
} else System.out.println(1000 + (dice1 * 100));
} else if ((dice1 == dice3)) {
if (dice1 == dice2) {
System.out.println(10000 + (dice1 * 1000));
} else System.out.println(1000 + (dice1 * 100));
} else if ((dice2 == dice3)) {
if (dice1 == dice2) {
System.out.println(10000 + (dice2 * 1000));
} else System.out.println(1000 + (dice2 * 100));
} else {
int max = dice1;
if (dice2 > dice1) {
max = dice2;
if (dice3 > dice2) {
max = dice3;
}
} else if (dice3 > dice1) {
max = dice3;
}
System.out.println(100 * max);
}
}
}
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));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int dice1 = Integer.parseInt(st.nextToken());
int dice2 = Integer.parseInt(st.nextToken());
int dice3 = Integer.parseInt(st.nextToken());
if ((dice1 == dice2)) {
if (dice1 == dice3) {
System.out.println(10000 + (dice1 * 1000));
} else System.out.println(1000 + (dice1 * 100));
} else if ((dice1 == dice3)) {
if (dice1 == dice2) {
System.out.println(10000 + (dice1 * 1000));
} else System.out.println(1000 + (dice1 * 100));
} else if ((dice2 == dice3)) {
if (dice1 == dice2) {
System.out.println(10000 + (dice2 * 1000));
} else System.out.println(1000 + (dice2 * 100));
} else {
int max = dice1;
if (dice2 > dice1) {
max = dice2;
if (dice3 > dice2) {
max = dice3;
}
} else if (dice3 > dice1) {
max = dice3;
}
System.out.println(100 * max);
}
}
}
- 내 풀이 너무 지저분하다!! 조건문 더 간략하게 바꾸고,
주사위의 모든 눈이 다를 경우 math.max() 함수를 사용해 가장 큰 수를 뽑아내보자.
Math.max( ) / Math.min()
- max( ) 함수는 두 인자 값 중 큰 값을 return 하는 메서드이다.
- min( ) 함수는 두 인자 값 중 작은 값을 return 하는 메서드이다.
- a 값과 b 값 중 큰 값을 뽑아내기 위해서
- Math.max(a,b)
- a, b, c 세 값 중 큰 값을 뽑아내고자 하는 경우는
- Math.max(Math.max((a,b),c))
삼항연산자
- 또 다른 풀이로 삼항연산자를 통해 큰 값을 뽑아낼 수도 있다.
- (조건문) ? (조건문이 참일 때 결과값) : (조건문이 거짓일 때 결과값)
- 삼항연산자는 중첩으로 사용 가능하다.
- (조건문1) ? (조건문1 참 결과값) : (조건문1 거짓일 때 수행할 조건문2) ? (조건문2참 결과값) : (조건문2 거짓 결과값)
- a 값과 b 값 중 큰 값을 뽑아내기 위해서
- (a>b) ? a : b
- a, b, c 세 값 중 큰 값을 뽑아내고자 하는 경우에는
- (a>b)&&(a>c) ? a : (b>c) ? b : c
수정한 풀이
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));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int dice1 = Integer.parseInt(st.nextToken());
int dice2 = Integer.parseInt(st.nextToken());
int dice3 = Integer.parseInt(st.nextToken());
if ((dice1 == dice2) && (dice1 == dice3)) {
System.out.println(10000 + dice1 * 1000);
} else if ((dice1 == dice2) || (dice1 == dice3)) {
System.out.println(1000 + dice1 * 100);
} else if (dice2 == dice3) {
System.out.println(1000 + dice2 * 100);
}else {
int max = Math.max(Math.max(dice1,dice2),dice3);
System.out.println(max*100);
}
}
}
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));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int dice1 = Integer.parseInt(st.nextToken());
int dice2 = Integer.parseInt(st.nextToken());
int dice3 = Integer.parseInt(st.nextToken());
if ((dice1 == dice2) && (dice1 == dice3)) {
System.out.println(10000 + dice1 * 1000);
} else if ((dice1 == dice2) || (dice1 == dice3)) {
System.out.println(1000 + dice1 * 100);
} else if (dice2 == dice3) {
System.out.println(1000 + dice2 * 100);
}else {
int max = Math.max(Math.max(dice1,dice2),dice3);
System.out.println(max*100);
}
}
}