[java] 2798번 블랙잭

ideal dev·2022년 12월 19일
0

코딩테스트

목록 보기
43/69
post-thumbnail

1. 문제 링크 및 문제

https://www.acmicpc.net/problem/2798

1-1 문제 요약

: 카드 3장의 합,근데 이제 M을 넘지 않는.

2. 해결 방법 생각해보자 ...

  1. 카드 3장의 전체 경우의 수를 구해야 함 = 백트래킹
    1-1 방법 : 3중 반복문을 돌려 중복없이 3장의 카드의 합을 구함
    1-2 중복없이 == 1,2,3의 반복문의 시작지점을 다르게 설정

3. 코드

import java.util.*;
import java.io.*;

public class Main {

    static int N,M;
    static int[] arr;
    static Boolean[] visited;
    static int MaxResult = Integer.MIN_VALUE;

    public static void main(String[] args) throws IOException{
        // 값 입력받기 -->
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        arr = new int[N];
        st = new StringTokenizer(br.readLine());
        for(int i=0;i<N;i++){
            arr[i] = Integer.parseInt(st.nextToken());
        }
        //<-- 

        for(int i=0;i<N;i++){
            for(int j=i+1;j<N;j++){
                for(int k=j+1;k<N;k++){
                    int ans = arr[i] + arr[j] + arr[k];
                    if (ans> MaxResult && ans<=M) {
                        MaxResult = Math.max(MaxResult,ans);
                    }
                }
            }
        }

        System.out.println(MaxResult);

    }
}

썸넬출처 : https://m.post.naver.com/viewer/postView.nhn?volumeNo=14677615&memberNo=5733106

0개의 댓글