简体   繁体   English

将集合平均分配到新阵列中

[英]Evenly distribute collection into new Arrays

I'm trying to create an method to evenly distribute an array into X numbers of new arrays, where there is only allowed 15 items pr array, and you are only allowed to create a new array, if the previous have 10 items, except if the array has less than 10 items. 我正在尝试创建一种将数组平均分配到X个新数组中的方法,其中仅允许15个项目pr数组,并且如果前一个数组有10个项目,则只允许创建一个新数组,除非阵列中的项目少于10个。

EDIT 编辑

To make my question more understandable for future readers. 使我的问题对将来的读者更易理解。

  • This is just like a fabric. 这就像一种织物。
  • You need to build X number of products. 您需要构建X个产品。
  • One product takes T amount to build for an employee. 一种产品需要T量才能为员工建造。

How many employees do you need and how do you share the work load between them? 您需要多少名员工,以及您如何分担他们的工作量?

END EDIT 结束编辑

 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]

I'm trying to solve this in C#. 我正在尝试用C#解决这个问题。

This is my code so far, but it fails at numbers like 16 = [16], 29 = [19][10], 38 = [18][10][10] 到目前为止,这是我的代码,但是在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();

Erm ... 嗯...

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;
}

seems to do what you want. 似乎可以满足您的要求。

Fun question. 好玩的问题。 I've given it a go: 我试了一下:

    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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM