Esempio n. 1
0
        public void PrintBottomUpCutRodWithSolution(int size)
        {
            RevenuesAndSize result = BottomUpCutRodWithSolution(size);

            while (size > 0)
            {
                Console.WriteLine(result.Sizes[size - 1]);
                size -= result.Sizes[size - 1];
            }
        }
Esempio n. 2
0
 private RevenuesAndSize BottomUpCutRodWithSolution(int size)
 {
     RevenuesAndSize result = new RevenuesAndSize() { Revenues = new int[size + 1], Sizes = new int[size + 1] };
     result.Revenues[0] = 0;
     for (int i = 1; i < size + 1; i++)
     {
         int maxRevenue = int.MinValue;
         for (int j = 1; j <= i; j++)
         {
             if (maxRevenue < prices[j - 1] + result.Revenues[i - j])
             {
                 maxRevenue = prices[j - 1] + result.Revenues[i - j];
                 result.Sizes[i] = j;
             }
         }
         result.Revenues[i] = maxRevenue;
     }
     return result;
 }
Esempio n. 3
0
        private RevenuesAndSize BottomUpCutRodWithSolution(int size)
        {
            RevenuesAndSize result = new RevenuesAndSize()
            {
                Revenues = new int[size + 1], Sizes = new int[size + 1]
            };

            result.Revenues[0] = 0;
            for (int i = 1; i < size + 1; i++)
            {
                int maxRevenue = int.MinValue;
                for (int j = 1; j <= i; j++)
                {
                    if (maxRevenue < prices[j - 1] + result.Revenues[i - j])
                    {
                        maxRevenue      = prices[j - 1] + result.Revenues[i - j];
                        result.Sizes[i] = j;
                    }
                }
                result.Revenues[i] = maxRevenue;
            }
            return(result);
        }