C# 排序大小的函数问题

C# 排序大小的函数问题,c#,C#,所以我在写一个小小的c#控制台应用程序,它应该是模仿某种购物系统。 所有产品均以包装形式出售(不单独出售),包装尺寸为4件、10件或15件 如果Qty大小为28,则应返回以下结果: 2 x 10包+2 x 4包。 或者给定一个数量大小(如25),它应该返回如下结果: 1 x 15包+1 x 10包 而且函数需要高效,因此它返回的包数最少。 (显然,忽略scenerio's,因为没有那么小的包装尺寸) 这是我目前的代码: 数量=28,大包装=15,中包装=10,小包装=4

所以我在写一个小小的c#控制台应用程序,它应该是模仿某种购物系统。 所有产品均以包装形式出售(不单独出售),包装尺寸为4件、10件或15件

如果Qty大小为28,则应返回以下结果: 2 x 10包+2 x 4包。 或者给定一个数量大小(如25),它应该返回如下结果: 1 x 15包+1 x 10包 而且函数需要高效,因此它返回的包数最少。 (显然,忽略scenerio's,因为没有那么小的包装尺寸)

这是我目前的代码:
数量=28,大包装=15,中包装=10,小包装=4

            double total = 0.00;
            int tmpQty = qty;

            while (tmpQty != 0)
            {
                if ((tmpQty >= LargePack) && ((tmpQty % LargePack) % 1) == 0)
                {
                    tmpQty -= LargePack;
                    lg += 1;
                    total =+ (lg * LargePrice);
                }
                else if ((tmpQty >= MediumPack)) 
                {
                    tmpQty -= MediumPack;
                    md += 1;
                    total =+ (md * MediumPrice);
                }
                else if ((SmallPack !=0) && (tmpQty >= SmallPack) && (tmpQty < MediumPack))
                {
                    tmpQty -= SmallPack;
                    sm += 1;
                    total =+ (sm * SmallPrice);
                }
            }
总是等于0-所以这是真的。。。当它应该是假的时候

或者,如果有人有更好的方法来计算包装尺寸,我愿意接受建议。 注意-这里没有定义包装尺寸,因为不同的产品使用相同的分拣过程

(任何内容%1)始终返回0。这就是为什么你的情况不起作用

试试这个

(tmpQty % LargePack) >= 0 
使用:


这将仅对可被LargePack整除的值返回true,因此在这种情况下,28将失败并继续前进。

您最大的问题是不了解%的作用。它不返回小数点。它的工作原理与小学部的“余数”类似。7%5将返回2。如果您想保持相同的逻辑(期望百分比),那么您需要除法,但您的变量需要是双倍或浮点。如果不是,C#将把结果转换为一个整数,该整数没有小数。我希望这能有所帮助。

这里有一个暴力方法。首先,我们需要一种方法来生成序列数组的所有组合。它被调用,实现如下所示:

public static IEnumerable<T[]> CartesianProduct<T>(IEnumerable<T>[] sequences)
{
    IEnumerable<IEnumerable<T>> seed = new[] { new T[0] };
    return sequences.Aggregate(
        seed,
        (accumulator, sequence) => accumulator
            .SelectMany(_ => sequence, (product, i) => product.Append(i))
    ).Select(product => product.ToArray());
}
最后一步是选择最佳组合。以下是实现所有这些目标的方法:

private static (int PackSize, int Quantity)[] GetBestCombination(
    int[] packSizes, int quantity)
{
    var sequences = packSizes
        .Select(p => Enumerable.Range(0, (quantity / p) + 1)
            .Select(q => p * q))
        .ToArray();
    var cartesianProduct = CartesianProduct(sequences);
    int[] selected = null;
    int maxValue = Int32.MinValue;
    foreach (var combination in cartesianProduct)
    {
        int sum = combination.Sum();
        if (sum > quantity) continue;
        if (sum > maxValue)
        {
            maxValue = sum;
            selected = combination;
        }
    }
    if (selected == null) return null;
    return selected.Zip(packSizes, (sum, p) => (p, sum / p)).ToArray();
}
让我们测试一下:

var bestCombo = GetBestCombination(new[] { 5, 8 }, 21);
foreach (var pair in bestCombo)
{
    Console.WriteLine($"{pair.Quantity} x {pair.PackSize}");
}
输出:

1 x 5
2 x 8


Remove
%1
-每个整数都可以被1整除。(tmpQty%LargePack)>=0不起作用,因为对于大于0的任何数字,它总是返回true。(tmpQty%LargePack)应该返回一个1.86的数字,因为这不是一个整数,所以我希望它返回false。@Blissol为什么你认为
tmpQty%LargePack
(整数修改操作)应该返回一个浮点数?@JohnLord yes。。“(任何%1)始终返回0”这似乎是一个有用的解决方案,但在“GetBestComposition”中似乎有一些错误?@Blissol什么类型的错误?你有没有得到意想不到的结果?
0*5 + 0*8 = 0
0*5 + 1*8 = 8
0*5 + 2*8 = 16
1*5 + 0*8 = 5
1*5 + 1*8 = 13
1*5 + 2*8 = 21
2*5 + 0*8 = 10
2*5 + 1*8 = 18
2*5 + 2*8 = 26
3*5 + 0*8 = 15
3*5 + 1*8 = 23
3*5 + 2*8 = 31
4*5 + 0*8 = 20
4*5 + 1*8 = 28
4*5 + 2*8 = 36
private static (int PackSize, int Quantity)[] GetBestCombination(
    int[] packSizes, int quantity)
{
    var sequences = packSizes
        .Select(p => Enumerable.Range(0, (quantity / p) + 1)
            .Select(q => p * q))
        .ToArray();
    var cartesianProduct = CartesianProduct(sequences);
    int[] selected = null;
    int maxValue = Int32.MinValue;
    foreach (var combination in cartesianProduct)
    {
        int sum = combination.Sum();
        if (sum > quantity) continue;
        if (sum > maxValue)
        {
            maxValue = sum;
            selected = combination;
        }
    }
    if (selected == null) return null;
    return selected.Zip(packSizes, (sum, p) => (p, sum / p)).ToArray();
}
var bestCombo = GetBestCombination(new[] { 5, 8 }, 21);
foreach (var pair in bestCombo)
{
    Console.WriteLine($"{pair.Quantity} x {pair.PackSize}");
}