백준 2839
처음엔 너무 쉬운 두문제를 풀어서 두개는 건너뛰고 기록한다.
https://www.acmicpc.net/problem/2839
알고리즘 분류는 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 |