Algorithm/BOJ

C#) [BOJ] 4796 캠핑

HSH12345 2023. 2. 1. 16:21

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

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

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

            List<int[]> arrList = new List<int[]>();

            while (true)
            {
                int[] arr = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
                if (arr[0] == 0 && arr[1] == 0 && arr[2] == 0) break;
                arrList.Add(arr);
            }//입력

            int max = 0;
            List<int> cntList = new List<int>();

            for(int i = 0; i < arrList.Count; i++)
            {
                max = arrList[i][2];
                int cnt = 0;
                while(max != 0)
                {
                    for(int j = 0; j < arrList[i][0]; j++)
                    {
                        if (max == 0) break;
                        cnt++;
                        max--;                        
                    }

                    for (int j = 0; j < arrList[i][1] - arrList[i][0]; j++)
                    {
                        if (max == 0) break;
                        max--;
                        
                    }
                }
                cntList.Add(cnt);                
            }

            for(int i = 0; i < cntList.Count; i++)
            {
                sw.WriteLine("Case {0}: {1}", i + 1, cntList[i]);
            }
            sr.Close();
            sw.Close();
        }
    }
}

 

시간초과

 

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

namespace _230201_1
{
    class Program
    {
        static void Main(string[] args)
        {

            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            List<int> ansList = new List<int>();

            while (true)
            {
                int[] arr = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
                if (arr[0] == 0 && arr[1] == 0 && arr[2] == 0) break;

                ansList.Add(arr[0] * (arr[2] / arr[1]) + Math.Min(arr[0], arr[2] % arr[1]));
            }//입력


            for (int i = 0; i < ansList.Count; i++)
            {
                sw.WriteLine("Case {0}: {1}", i + 1, ansList[i]);
            }
            sr.Close();
            sw.Close();
        }
    }
}

 

 결국 해결하려면 수학적 공식을 찾아내야한다. 해답에대 한 공식은 ans =  L * (V / P) + Math.Min(L, (V % P) 이다. 왜 인지는 모르겠다...