I have multiple nested loops that is equal to amount of data. For example, find the bar cutting combinations of 3 given lengths. so that the waste should not be longer than 1' after cutting in the 20' length rebar. so, I used 3 nested loops because there are 3 bar cuts. If I have 50 bar Cuts, then I will use 50 nested loops. So, what is the solution to this problem so no need to edit the source code. maybe double recursion? or any other algorithm. Thanks. Sample answer to given data below. enter image description here

 for (int a = 0; a < 50; a++){
     for (int b = 0; b < 50; b++){
        for (int c = 0; c < 50; c++){
            double TL = a * BarCut[0] + b * BarCut[1] + c * BarCut[2];
            double waste = 20 - TL;
                //if (waste >= 0 && waste < 1 )
                    //Console.WriteLine($"{a}A, {b}B, {c}C, TL={TL}, waste = {waste:F2}");
        }
     }
 }

推荐答案

在阅读了一些关于递归函数的多嵌套循环的主题后.我得到了最终的解决方案,我从其他线程复制了一些行,但添加了一些过滤数据的条件.下面是条形组合切割的代码和答案,以满足不再浪费1‘,但我的最终目标是https://en.wikipedia.org/wiki/Cutting_stock_problem.

    private static double[] BarCuts = { 6.2, 5, 3, 4};  // List of Bar Cuttings.
    static void Main(string[] args)
    {
        List<int[]> collection = new List<int[]>();
        int[] limits = GetLimits(BarCuts); 
        printHading(BarCuts);
        
        AddToCollectionRecursive(collection, limits);
        
        foreach (var item in collection)
            printResult(item); 
        Console.ReadLine();
    }

    static void AddToCollectionRecursive(List<int[]> collection, params int[] limits)
    {
        AddTo(collection, new List<int>(), limits, limits.Length - 1);
    }

    static void AddTo(List<int[]> collection, IEnumerable<int> value, IEnumerable<int> counts, int left)
    {
        for (var i = 0; i < counts.First(); i++)
        {
            var list = value.ToList();
            list.Add(i);
            if (left == 0)
            {
                double tl = Combination_Length(list);
                if (tl > 20) { break; }

                double waste = 20 - tl;          // 20 = avlb bar length.
                if (waste < 1)
                    collection.Add(list.ToArray());
            }
            else
            {
                AddTo(collection, list, counts.Skip(1), left - 1);
            }
        }
    }

    static double Combination_Length(List<int> CutsQty)
    {
        double tl = 0;
        for (int j = 0; j < CutsQty.Count; j++)
            tl += CutsQty[j] * BarCuts[j];
        return tl;
    }

答案如下.

A = 6.2 , B = 5 , C = 3 , D = 4

5D = TL= 20,废物= 0.00

4C&;2D=>;TL=20,废物=0.00

1B&;1C&;3D=>;TL=20,废物=0.00

1B&A;5C=>;TL=20,废物=0.00

2B&;2C&;1D=>;TL=20,浪费=0.00

4B=>;TL=20,废物=0.00

1A&;3C&;1D=>;TL=19.2,废物=0.80

1A&;1B&;2D=>;TL=19.2,废物=0.80

1A 2B 1C = TL= 19.2,废物= 0.80

2a&;1C&;1D=>;TL=19.4,废物=0.60

Csharp相关问答推荐

在Microsoft XNA框架(MonoGame)中旋转相机

禁用AutoSuggestBox项目更改时的动画?

注册通用工厂的C# Dep注入

哪个nuget包含SecurityStampValidatorOptions

并行令牌更新

try 还原包时出错:Dapper已经为System.Data.SQLClient定义了依赖项''''

从Blob存储中提取tar.gz文件并将提取结果上载到另一个Blob存储

将现有字段映射到EFCore中的复杂类型

C#EF Core WHERE IN LINQ FROM LIST WITH.CONTAINS不返回任何内容

静态对象构造顺序

从.Net 6 DLL注册和检索COM对象(Typelib导出:类型库未注册.(异常来自HRESULT:0x80131165))

Rx.Net窗口内部可观测数据提前完成

如何在Cosmos SDK中控制超时、重试和重试之间的延迟?

使用ExtractIconEx(或其他方式)提取最大的可用图标

自定义列表按字符串的部分排序

无法将生产环境的AppDbContext设置替换为用于集成测试的内存数据库

MudBlazor Textfield已禁用,但其验证工作正常

C#中类库项目的源代码生成器

C#LINQ多行条件

无法停止PowerShell中的低级挂钩(c#挂钩)