백준 2839

Algorithm|2023. 8. 18. 19:32

처음엔 너무 쉬운 두문제를 풀어서 두개는 건너뛰고 기록한다.

 

https://www.acmicpc.net/problem/2839

 

2839번: 설탕 배달

상근이는 요즘 설탕공장에서 설탕을 배달하고 있다. 상근이는 지금 사탕가게에 설탕을 정확하게 N킬로그램을 배달해야 한다. 설탕공장에서 만드는 설탕은 봉지에 담겨져 있다. 봉지는 3킬로그

www.acmicpc.net

알고리즘 분류는 DP와 Greedy인데 어떤 알고리즘이다 생각없이 그냥 막 푼것같다.

일단 절대 DP로 푼건 아니다.

 

using System;
using System.Collections.Generic;

namespace Sugar_Delivery
{
    class b2839
    {
        static void Main(string[] args)
        {
            int val = int.Parse(Console.ReadLine());

            Console.WriteLine(solution(val));

        }

        static int solution(int sugar)
        {
            int sol = sugar;
            int cnt = 0;

            for(int i = 0; i <= (sugar / 5); i++)
            {
                // i = big plastic bag num
                for(int j = 0; j <= (sugar - (5 * i)); j++)
                {
                    // j = small plastic bag num
                    if (((5 * i + 3 * j) == sugar) && i + j < sol)
                    {
                        sol = i + j;
                        cnt++;
                    }
                }
            }

            if (cnt == 0)
                return -1;
            else
                return sol;
        }
    }
}

'Algorithm' 카테고리의 다른 글

백준 1149  (0) 2023.08.20
백준 11866  (0) 2023.08.19
백준 12789  (0) 2023.08.19
백준 2798  (0) 2023.08.18
시작한 계기  (0) 2023.08.18

댓글()