[Java | 백준 2798] 블랙잭
· 테크· Java
Java
백준 2798 www.acmicpc.net/problem/2798
풀이
모든 경우의 수를 고려하는 브루트 포스 문제이다. 주어진 카드에서 3장의 수를 더해서 그 값이 입력받은 value 값을 넘지 않는 최대 합을 구하는 문제이다.
세 가지 정수를 계산하기 위해 중첩 for 문을 이용해서 풀이했다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cardnum = sc.nextInt();
int value = sc.nextInt();
int max = 0, sum = 0;
int[] intArr = new int[cardnum];
for (int i=0; i<intArr.length; i++) {
intArr[i]=sc.nextInt();
}
sc.nextLine();
for (int i=0; i<intArr.length-2; i++) {
for (int j=i+1; j<intArr.length-1; j++) {
for (int l=j+1; l<intArr.length; l++) {
sum = intArr[i]+intArr[j]+intArr[l];
if (sum<=value) max = Math.max(max,sum);
}
}
}
System.out.print(max);
sc.close();
}
}
|
cs |
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?
1–5 / 319