Divide the number into groups of equal size

Using this example:

var amount = x;
var maxPerGroup = y;
var amountGroups = Ceiling(amount/maxPerGroup);

Can someone help me split the amount into total groups with maximum amount per maxAmount group? These groups should be almost the same size.

For example: quantity = 45; maxPerGroup = 15; amountGroups = 3;

Result: 15 15 15

I use C # as a language.

Thanks in advance!

+5
source share
6 answers

Note is not exact C # to give you this idea.

I think you are looking for a way to grammatically divide a number in different groups. Not knowing how large the groups are and the random number of groups.

so let's say x = 30 y = 15. 30/15 = 3 groups of 15 and let x = 43, so the number should look like? 14 14 15

groups (since you already have this calculated correctly)(should be a double)
//   maxPerGroup = y
membersPerGroup = floor(amount/groups)



List a = new List
//Is the leftover value of the modulus
leftover = amount%groups;
//Loops for each group
for(int i=0;i<groups;i++){


//If there is a left over value
if(leftover>0){
  a.Add(membersPerGroup +1);
  leftover--;
}else{
  a.Add(membersPerGroup );
}

}

#, ,

+1
number of groups := ceiling(total / max group size)
number per group := floor(total / number of groups)
rem = total % number per group

rem number per group + 1 number of groups - rem number per group.

EDIT: :

total := 50
max group size := 15
number of groups := ceiling(50 / 15) // 4
number per group := floor(50 / 4) // 12
rem := 50 % 12 // 2

2 13 2 12.

+3

simple non-optimized solution:

int i = amount;
int j = 0;
int [] groups = new  int[amountGroups];
while(i > 0) {
   groups[j] += 1;
   i--;
   j = (j+1)%amountGroups;
}
+1
source

There are many ways to split the amount between groups. It all depends on whether the number of groups is the only factor or whether there are other factors. Cm:

    static void Main(string[] args)
    {
        List<int> list1 = Split1(48, 15); // result is: 15, 15, 15, 3
        List<int> list2 = Split2(48, 15); // result is 12, 12, 12, 12
    }

    public static List<int> Split1 (int amount, int maxPerGroup)
    {
        int amountGroups = amount / maxPerGroup;

        if (amountGroups * maxPerGroup < amount)
        {
            amountGroups++;
        }

        List<int> result = new List<int>();
        for (int i = 0; i < amountGroups; i++)
        {
            result.Add(Math.Min(maxPerGroup, amount));
            amount -= Math.Min(maxPerGroup, amount);
        }
        return result;
    }

    public static List<int> Split2 (int amount, int maxPerGroup)
    {
        int amountGroups = amount / maxPerGroup;

        if (amountGroups * maxPerGroup < amount)
        {
            amountGroups++;
        }

        int groupsLeft = amountGroups;
        List<int> result = new List<int>();
        while (amount > 0)
        {
            int nextGroupValue = amount / groupsLeft;
            if (nextGroupValue * groupsLeft < amount)
            {
                nextGroupValue++;
            }
            result.Add(nextGroupValue);
            groupsLeft--;
            amount -= nextGroupValue;
        }
        return result;
    }
0
source
    // For separating a collection into ranges
    static List<List<T>> Split<T>(List<T> source, int size)
    {
        // TODO: Prepopulate with the right capacity
        List<List<T>> ret = new List<List<T>>();
        for (int i = 0; i < source.Count; i += size)
        {
            ret.Add(source.GetRange(i, Math.Min(size, source.Count - i)));
        }
        return ret;
    }

    // For separating an int into a Tuple range
    static List<Tuple<int, int>> Split(int source, int size)
    {
        var ret = new List<Tuple<int, int>>();
        for (int i = 0; i < source; i += size)
        {
            ret.Add(new Tuple<int, int>(i, (i + Math.Min(size, source - i))));
        }
        return ret;
    }
0
source
int amount = x;
int maxPerGroup = y;
int amountGroups = new int[Ceiling(amount/maxPerGroup)];
for(int i=0; i<maxPerGroup; i++)
{
    if(x>maxPerGroup)
    {
        amountGroups[i]= maxPerGroup;
        x = x-maxPerGroup;
    }
    else
    {
        amountGroups[i] = x;
        x =0;
    }
}
-2
source

All Articles