2839번: 설탕 배달 (acmicpc.net)

 

2839번: 설탕 배달

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

www.acmicpc.net

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace _03_2839
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int N = int.Parse(sr.ReadLine());
            int ans = 0;

            if(3 <= N && N <= 5000)
            {
                while(N >= 0)
                {
                    if (N % 5 == 0) //5로 나누어 떨어지는 경우 그대로 종료
                    {
                        ans += N / 5;
                        N = 0; //뒤의 if문을 활용하기 위해
                        break;
                    }

                    N -= 3;  //3kg 가방에 한번 옮기고 다시 5로 나눈다.
                    ans += 1;
                }
                if (N != 0) ans = -1; //남은 수를 전부 3으로 빼서 N은 0이되거나 N이 5로 나누어 떨어진다면 강제로 0으로 만든다.
                sw.WriteLine(ans);
            }            

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

 

수학적 규칙 숙지 필요함

 

'Algorithm > BOJ' 카테고리의 다른 글

C#) [BOJ] 2591 숫자 카드 2  (0) 2023.01.25
C#) [BOJ] 11650 좌표 정렬하기  (0) 2023.01.25
C#) [BOJ] 1978 소수찾기  (0) 2023.01.25
C#) [BOJ] 2292 벌집 ★  (0) 2023.01.25
C#) [BOJ] 9375 패션왕 신해빈 ★  (1) 2023.01.24

+ Recent posts