백준 algorithm
백준 2798 - 블랙잭
cosmohoo
2019. 10. 2. 00:21
반응형

브루트 포스로 검색을 하면서 조건에 맞는 값을 구하면 되는 문제이다.
다른 포스트들을 보니 DFS로 하는 방법 역시 있는데, 공부를 해봐야겠다.
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#define Max_num 3
using namespace std;
int main()
{
int N, M;
cin >> N; //카드의 갯수
cin >> M;
vector <int> Card;
int answer=0;
for (int i = 0; i < N; i++)
{
int tem;
cin >> tem;
Card.push_back(tem);
}
for (int a = 0; a < Card.size() - 2; a++)
{
for (int b = a+1; b < Card.size() - 1; b++)
{
for (int c = b+1; c < Card.size() ; c++)
{
int temp_2 = Card[a] + Card[b ] + Card[c ];
if (answer == 0 && temp_2 <= M)//초기 answer값 배정
{
answer = temp_2;
}
else if (answer != 0 && temp_2 <= M)
{
answer = max(answer, temp_2);
}
}
}
}
cout << answer;
}반응형