본문 바로가기
알고리즘/백준 BOJ

[BOJ C#] 2798 블랙잭

by 왹져박사 2023. 1. 25.
728x90

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace _2798
{
    class Program
    {
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            string[] nm = sr.ReadLine().Split(' ');
            int n = int.Parse(nm[0]);
            int m = int.Parse(nm[1]);

            string[] cardString = sr.ReadLine().Split(' ');
            int[] cardInt = new int[cardString.Length];
            int sum = 0;
            bool blackJack = false;
            List<int> sums = new List<int>();

            for(int i = 0; i < cardString.Length; i++)
            {
                cardInt[i] = int.Parse(cardString[i]);
            }
            for(int card1 = 0; card1 < cardInt.Length-2; card1++)
            {
                for (int card2 = card1+1; card2 < cardInt.Length-1; card2++) 
                {
                    for (int card3 = card2+1; card3 < cardInt.Length; card3++)
                    {
                        sum = cardInt[card1] + cardInt[card2] + cardInt[card3];

                        if (sum == m)
                        {
                            blackJack = true;
                            break;
                        }
                        else if (sum < m)
                        {
                            sums.Add(sum);
                        }
                    }

                }
            }
            if (blackJack)
                sw.WriteLine(m);
            else
                sw.WriteLine(sums.Max());

            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

728x90

'알고리즘 > 백준 BOJ' 카테고리의 다른 글

[BOJ C#] 4153 직각삼각형  (0) 2023.01.25
[BOJ C#] 10250 ACM 호텔  (0) 2023.01.25
[BOJ C#] 2775 부녀회장이 될테야  (0) 2023.01.25
[BOJ C#]11659 구간 합 구하기 4  (0) 2023.01.16
[BOJ C#] 11720 숫자의 합  (0) 2023.01.15