ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 2798 - 블랙잭
    백준 algorithm 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;
    	
    }

    '백준 algorithm' 카테고리의 다른 글

    백준 1978 - 소수찾기  (0) 2019.10.30
    백준 10869 - 사칙연산  (0) 2019.10.02
    백준 1676 - 팩토리얼 0의 개수  (0) 2019.09.29
    백준 2581 - 소수  (0) 2019.09.27
    백준 10773 - 제로  (0) 2019.09.25

    댓글

Designed by Who.