728x90
문제
N명의 인원이 각각 3번씩 주사위를 굴려 다음 규칙에 따른 값을 가진다.
같은 눈이 3개가 나오면 10,000원+(같은 눈)×1,000원의 상금을 받게 된다.
같은 눈이 2개만 나오는 경우에는 1,000원+(같은 눈)×100원의 상금을 받게 된다.
모두 다른 눈이 나오는 경우에는 (그 중 가장 큰 눈)×100원의 상금을 받게 된다.
N명이 갖는 값중 최대값을 출력하기.
문제링크 : https://www.acmicpc.net/problem/2476
원리
* 리스트에 중복될 눈이 있다면, 담지 않음
* 다른 눈이라면 리스트에 담음
* 리스트 사이즈에 따라, 상태는 다음과 같음
* 리스트 사이즈 1 = 전체 중복
* 리스트 사이즈 2 = 두개 중복
* 리스트 사이즈 3 = 모두 다름
*
* 따라서, 각 케이스에 맞춰 계산 진행.
* 이때, 중복된 눈이 있다면, 따로 메모라이즈
나의 코드
import java.io.*;
import java.util.*;
public class no2476 {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int N;
private static ArrayList<Integer> members = new ArrayList<>(); // 결과 값들을 담을 리스트
private static StringTokenizer st;
public static void main(String[] args) throws IOException {
N = Integer.parseInt(br.readLine());
// 각 인원수별 조사 반복
while(N-->0) {
List<Integer> temp_list = new ArrayList<>(); // 주사위 눈을 담을 리스트
st = new StringTokenizer(br.readLine(), " ");
int fst = Integer.parseInt(st.nextToken());
int sec = Integer.parseInt(st.nextToken());
int trd = Integer.parseInt(st.nextToken());
temp_list.add(fst); // 첫 눈금은 비교를 위해 무조건 담음
int memory = fst;
//두번째 눈금 비교
if(!temp_list.contains(sec)) temp_list.add(sec);
else memory = sec;
//세번째 눈금 비교
if(!temp_list.contains(trd)) temp_list.add(trd);
else memory = trd;
// 각 케이스별 결과 계산 후 저장
int result;
if(temp_list.size() == 1) result = 10000 + (memory * 1000);
else if(temp_list.size() == 2) result = 1000 + (memory * 100);
else result = Collections.max(temp_list) * 100;
members.add(result);
}
// 결과들 중 최대값 출력
System.out.println(Collections.max(members));
}
}
728x90
'알고리즘 저장소 (일반방식과 나만의 풀이) > JAVA' 카테고리의 다른 글
[백준] no5639:이진 검색 트리 (0) | 2023.09.03 |
---|---|
[백준] no2712: 미국스타일 - 구현 (0) | 2023.08.31 |
[백준] no6887:Squares - 제곱근 구하기 (0) | 2023.08.28 |
[백준] no11504:바이토닉 수열 - 바이토닉 정렬(Bitonic) (0) | 2023.08.25 |
[백준] no2638:치즈 - BFS, DFS (0) | 2023.08.24 |