Algorithm_BOJ(백준)/완전탐색(Brute Force)
[백준 2798 c++] 블랙잭
xhaktmchl
2021. 2. 8. 02:25
728x90
반응형
문제 링크
2798번: 블랙잭
첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장
www.acmicpc.net
문제 접근
// 접근: 3장의 카드의 합 이니깐 3중반복 완전탐색
// 접근 2: dfs로 생각했으나 완전탐색이 더 간단할 것 같아서 않함
문제 풀이
// 풀이: 3중반복으로 완전탐색하며 m을 넘지않는 최대합 저장
주의
개념
소스코드
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio> // c 문법 헤더파일
#include<string> // c++ 문자열 클래스
#include<vector> // 동적배열 라이브러리
#include<stack>
#include<queue>
#include<algorithm> // sort와 unique 사용
#include<cmath> // 제곱이나 루트함수 사용
#include<cstring> // memset 함수
#include <set>
#include <map> // map구조체
#include <numeric> //accumulate(v.begin(), v.end(), 0);
// [백준 2798 c++] 블랙잭
// 접근: 3장의 카드의 합 이니깐 3중반복 완전탐색
// 접근 2: dfs로 생각했으나 완전탐색이 더 간단할 것 같아서 않함
// 풀이: 3중반복으로 완전탐색하며 m을 넘지않는 최대합 저장
using namespace std; // cin,cout 편하게 사용 라이브러리
int n;
int m;
int ar[101];
int result = 0;
int main()
{
// IO 속도 향상
//ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n>>m;
for (int i= 0; i < n; i++)
{
cin >> ar[i];
}
int max = ar[0];
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
for (int k = j + 1; k < n; k++)
{
int sum = ar[i] + ar[j] + ar[k];
if (sum >= max && sum <= m) { max = sum; }
}
}
}
cout << max;
return 0;
}
반응형