将数字拆分为相同大小的组

时间:2012-07-02 12:53:11

标签: c# math

使用此示例:

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

有人可以帮我解决如何使用每组maxAmount的最大金额将金额拆分为金额组吗? 这些组的大小几乎相同。

例如:     金额= 45;     maxPerGroup = 15;     amountGroups = 3;

结果:1​​5 15 15

我使用C#作为语言。

提前致谢!

9 个答案:

答案 0 :(得分:3)

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

您将remnumber per group + 1组和number of groups - remnumber per group。{/ p>

编辑:示例:

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。

答案 1 :(得分:2)

分组之间的数量有很多种方法。这一切都取决于唯一因素是群体数量还是其他因素。参见:

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

答案 2 :(得分:1)

简单的非优化解决方案:

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

答案 3 :(得分: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#编写,但似乎你找到了正确的代码

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

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

        }
    }

答案 6 :(得分:0)

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

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

所有功劳都归功于 Sachin。

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

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