7

使用这个例子:

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

有人可以帮助我如何将 Amount 拆分为 AmountGroups,每组 maxAmount 的最大数量是多少?这些组的大小必须几乎相同。

例如:金额 = 45;maxPerGroup = 15; 数量组 = 3;

结果:15 15 15

我使用 C# 作为语言。

提前致谢!

4

9 回答 9

3
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 + 1number of groups - remnumber per group

编辑:示例:

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 组。

于 2012-07-02T13:00:50.580 回答
3

有很多方法可以在组之间分配金额。这完全取决于唯一的因素是组数还是其他因素。看:

    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;
    }
于 2012-07-02T13:15:34.707 回答
1

简单的非优化解决方案:

int i = amount;
int j = 0;
int [] groups = new  int[amountGroups];
while(i > 0) {
   groups[j] += 1;
   i--;
   j = (j+1)%amountGroups;
}
于 2012-07-02T13:03:52.067 回答
1

注意 不是精确的 c# 只是为了给你一个想法。

我认为您正在寻找一种在语法上将数字划分为不同组的方法。在不知道这些组有多大和随机数量的组的情况下。

所以假设 x = 30 y = 15. 30/15 = 3 组 15 并且假设 x= 43 那么数字应该是这样的?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 );
}

}

我可以用正确的 c# 编写,但您似乎找到了正确的代码

于 2012-07-02T13:26:15.987 回答
1
private static int[] DistributeIntoGroups(int sum, int groupsCount)
{
    var baseCount = sum / groupsCount;
    var leftover = sum % groupsCount;
    var groups = new int[groupsCount];

    for (var i = 0; i < groupsCount; i++)
    {
        groups[i] = baseCount;
        if (leftover > 0)
        {
            groups[i]++;
            leftover--;
        }
    }

    return groups;
}
于 2021-06-14T21:25:14.957 回答
0
    // 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;
    }
于 2015-01-19T10:28:55.773 回答
0
private void button2_Click(object sender, EventArgs e)
    {
        // I wanted to get count from a datagridview (x)
        // and then split into groups based on the 
        // count from a combobox (n). In my example my
        // grid had 1771 rows and was split into: 25 groups
        // (4x of 70 and 21x of 71)

        // Driver code
        int x = myDataGridView.Rows.Count; //1771  
        int n = assemblies_cmbbox.Items.Count; //25

        split(x, n);
        //split(amount, maxPerGroup);
    }

    // Function that prints
    // the required sequence
    private void split(int x, int n)
    {

        // If we cannot split the
        // number into exactly 'N' parts
        if (x < n)
            Debug.WriteLine("-1 ");
        // If x % n == 0 then the minimum
        // difference is 0 and all
        // numbers are x / n
        else if (x % n == 0)
        {
            for (int i = 0; i < n; i++)
                Debug.WriteLine((x / n) + " ");

        }
        else
        {
            // upto n-(x % n) the values
            // will be x / n
            // after that the values
            // will be x / n + 1
            int zp = n - (x % n);
            int pp = x / n;
            for (int i = 0; i < n; i++)
            {

                if (i >= zp)
                    Debug.WriteLine((pp + 1) + " ");
                else
                    Debug.WriteLine(pp + " ");
            }
        }
    }

全部归功于萨钦。

访问https://www.geeksforgeeks.org/split-the-number-into-n-parts-such-that-difference-between-the-smallest-and-the-largest-part-is-minimum/

于 2021-07-30T00:03:38.877 回答
0

将一个整数分成几组

public class Program
    {
        static void Main(string[] args)
        {
            List<int> results = DistributeInteger(20, 3).ToList();//output: 7,7,6
            foreach (var result in results)
            {
                Console.WriteLine(result);
            }
            Console.Read();
        }

        public static IEnumerable<int> DistributeInteger(int total, int divider)
        {
            if (divider == 0)
                yield return 0;

            int rest = total % divider;
            double result = total / (double)divider;

            for (int i = 0; i < divider; i++)
            {
                if (rest-- > 0)
                    yield return (int)Math.Ceiling(result);
                else
                    yield return (int)Math.Floor(result);
            }

        }
    }
于 2020-07-17T10:12:18.657 回答
-2
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;
    }
}
于 2012-07-02T13:14:46.890 回答