2

我正在尝试创建一种将数组均匀分布到 X 个新数组中的方法,其中仅允许 15 个项目 pr 数组,并且您只允许创建一个新数组,如果前一个有 10 个项目,除非该数组的项目少于 10 个。

编辑

为了让未来的读者更容易理解我的问题。

  • 这就像一块织物。
  • 您需要构建 X 个产品。
  • 一个产品需要 T 金额为员工构建。

您需要多少员工,您如何分担他们之间的工作量?

结束编辑

 Max allowed number in array = 15;
 Min allowed number in array = 10;    

 Number = Numbers of Items in the Collection.

 Number  5 =>   [5]
 Number 13 =>   [13]
 Number 16 =>   [10] [6]
 Number 29 =>   [15] [14]
 Number 30 =>   [15] [15]
 Number 31 =>   [11] [10] [10]
 Number 32 =>   [12] [10] [10]
 Number 33 =>   [11] [11] [11]

我试图在 C# 中解决这个问题。

到目前为止,这是我的代码,但它在 16 = [16]、29 = [19][10]、38 = [18][10][10] 等数字处失败

const int maxAllowedOrderLines = 15;
const int minAllowedOrderLines = 10;
var optimalOrderDisp = new List<int>();

Console.WriteLine("Number of OrderLines");
int linjer = Convert.ToInt32(Console.ReadLine());

if (linjer <= maxAllowedOrderLines)
   optimalOrderDisp.Add(linjer);
else
{
   for (var i = maxAllowedOrderLines; i > 0; i--)
   {
      var maxOrderLines = linjer%i;
      if (maxOrderLines == 0 || i <= minAllowedOrderLines || linjer < maxAllowedOrderLines)
      {
         Console.WriteLine("Optimal number of order lines {0}--{1}", i, (double) linjer/(double) i);

          var optimalNumberOfOrders = linjer/i;
          for (var orderNumber = 0; orderNumber < optimalNumberOfOrders; orderNumber++)
          {
             optimalOrderDisp.Add(i);
          }

          if (maxOrderLines != 0)
             optimalOrderDisp[0] += maxOrderLines;
          break;
       }
    }
 }
 foreach (var i1 in optimalOrderDisp)
 {
    Console.Write("[{0}]", i1);
 }
 Console.WriteLine();
4

2 回答 2

5

嗯...

const double bucketSize = 15.0;
var totalItems = (double)linjer;
var optimumBuckets = Math.Ceiling(totalItems / bucketSize);
var itemsPerBucket = (int)Math.Ceiling(totalItems / optimumBuckets);

var buckets = new int[(int)optimumBuckets];

var itemsLeft = (int)totalItems
for (var i = 0; i < buckets.length; i++)
{
    if (itemsLeft < itemsPerBucket)
    {
        buckets[i] = itemsLeft;
    }
    else
    {
        buckets[i] = itemsPerBucket;
    }
    itemsLeft -= itemsPerBucket;
}

似乎做你想做的事。

于 2012-07-13T11:34:06.827 回答
2

有趣的问题。我试了一下:

    const int maxAllowedOrderLines = 15;
    const int minAllowedOrderLines = 10;

    static List<int> optimalOrderDisp = new List<int>();

    static void Main(string[] args)
    {
        int Lines = Convert.ToInt32(Console.ReadLine());

        int MinNumberOfBuckets = (int) Math.Ceiling((double) Lines / minAllowedOrderLines);
        int RemainingLines = Lines;
        int BucketLines = Lines / MinNumberOfBuckets;

        // Distribute evenly
        for (int i = 0; i < MinNumberOfBuckets; i++)
        {
            optimalOrderDisp.Add(i != MinNumberOfBuckets - 1 ? BucketLines : RemainingLines);
            RemainingLines -= BucketLines;
        }

        // Try to remove first bucket
        while (RemoveBucket())
        {
        }

        // Re-balance
        Lines = optimalOrderDisp.Sum();
        RemainingLines = Lines;
        BucketLines = (int) Math.Round((double) Lines / (optimalOrderDisp.Count));
        for (int i = 0; i < optimalOrderDisp.Count; i++)
        {
            optimalOrderDisp[i] = (i != optimalOrderDisp.Count - 1 ? BucketLines : RemainingLines);
            RemainingLines -= BucketLines;
        }

        // Re-balance to comply to min size
        for (int i = 0; i < optimalOrderDisp.Count - 1; i++)
            if (optimalOrderDisp[i] < minAllowedOrderLines)
            {
                int delta = minAllowedOrderLines - optimalOrderDisp[i];

                optimalOrderDisp[i] += delta;
                optimalOrderDisp[optimalOrderDisp.Count - 1] -= delta;
            }

        Console.WriteLine(String.Join("\n", optimalOrderDisp.ToArray()));
    }

    static bool RemoveBucket()
    {
        if (optimalOrderDisp.Sum() > maxAllowedOrderLines * (optimalOrderDisp.Count - 1))
            return false;

        int Lines = optimalOrderDisp[0];
        int RemainingLines = Lines;
        int BucketLines = Lines / (optimalOrderDisp.Count - 1);

        // Remove bucket and re-distribute content evenly
        // Distribute evenly
        for (int i = 1; i < optimalOrderDisp.Count; i++)
        {
            optimalOrderDisp[i] += (i != optimalOrderDisp.Count - 1 ? BucketLines : RemainingLines);
            RemainingLines -= BucketLines;
        }

        optimalOrderDisp.RemoveAt(0);
        return true;
    }
于 2012-07-13T11:30:38.340 回答