均匀地将集合分配到新的阵列中

时间:2012-07-13 10:54:41

标签: c# .net algorithm language-agnostic

我正在尝试创建一种方法,将数组均匀分布到X个新数组中,其中只允许15个项目数组,并且只允许创建一个新数组,如果之前有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();

2 个答案:

答案 0 :(得分: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;
}

似乎做你想做的事。

答案 1 :(得分: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;
    }