-
백준 2798 - 블랙잭(JAVA)Algorithm 2022. 9. 18. 14:39728x90
오늘은 백준 2798 블랙잭 문제를 가져와 보았다.
문제는 위 사이트를 참고하기 바란다. 처음에 입력해야 할 사항들이 많아서 조금 당황했는데 크기 N의 배열을 만들면 모든 입력을 쉽게 받을 수 있었다.
※ 알고리즘
딱히 큰 알고리즘이 필요가 없는 문제였다. 결국 순회를 하면서 세 개의 숫자를 선택하는 방식을 택하면 되기 때문에 전형적인 전체 순회 "브루트 포스" 문제임을 알 수 있었다.
1. 브루트 포스를 이용해서 3중 for문으로 구성한다.
2. sum 변수에 선택한 세 개의 값을 모두 담는다.
3. 근처의 값을 구할 수 있도록 하기 위해서는 바로 sum을 출력시키면 안 된다.
4. result 결과 변수를 하나 만들어 sum보다 작고 그 sum 이 M과 비교해서 작거나 같게 될 경우 result에 sum 값을 담아준다.
5. 결과 변수 출력
★ 주의 사항
그냥 아무 생각 없이 N까지 범위로 3중 for문을 세 개 만들었더니 런타임 오류가 떠 버렸다. 때문에 범위를 조금 줄이기 위해서 0~N-2, 1~N-1, 2~N 정도로 만들기 위해 for문 변수를 이용해 최종 범위를 선택하게 되면, 0~N-2, i+1~N-1, j+1~N 으로 결정한다.
이렇게 해결해 볼 수 있었다. 요즘 유독 브루트 포스 문제가 많이 선택되는 느낌이긴 한데 브루트 포스를 어떨때 정확히 사용할 수 있는지 반복해서 보게 되니 숙달이 좀 되는 듯 하다.
728x90'Algorithm' 카테고리의 다른 글
백준 2902 - KMP는 왜 KMP일까? (JAVA) (0) 2022.09.20 백준 1225 - 이상한 곱셈(JAVA) (0) 2022.09.20 백준 2231 - 분해합(JAVA) (0) 2022.09.16 백준 1173 - 운동(JAVA) (0) 2022.09.16 백준 2292 - 벌집(JAVA) (0) 2022.09.13