반응형
출처 : https://www.acmicpc.net/problem/2798
Problem
Code
package baek.bruteForce;
import java.io.*;
import java.util.*;
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 maxValue = 0;
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[] 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 - 2; i++) {
if (arr[i] > M)
continue;
for (int j = i + 1; j < N - 1; j++) {
if (arr[i] + arr[j] > M)
continue;
for (int k = j + 1; k < N; k++) {
int tempValue = arr[i] + arr[j] + arr[k];
if (M == tempValue) {
maxValue = tempValue;
break;
}
if (maxValue < tempValue && tempValue < M)
maxValue = tempValue;
}
}
}
System.out.println(maxValue);
}
}
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[완전 탐색] 2839번 설탕배달 (0) | 2023.06.08 |
---|---|
[완전탐색]1436번 영화감독 (0) | 2023.06.08 |
[완전탐색]1018번 체스판 다시칠하기 (0) | 2023.06.07 |
[완전탐색]19352번 수학은 비대면강의입니다. (0) | 2023.06.07 |
[완전탐색]2231번 분해합 (0) | 2023.06.06 |