Cartesian Product + N x M Dynamic Array

前端 未结 3 1858
心在旅途
心在旅途 2020-12-16 07:29

I have looked hours for a solution without any success. Hopefully someone can help me out.

I have a dynamic array of N items on M origin zip codes.

For insta

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 07:39

    What you're looking to do is generate combinations of each item in the array.

    Here's an example hard-coded for N == 3:

            var array1 = new[] { 1101, 5410, 60621 };
            var array2 = new[] { 1101, 60621 };
            var array3 = new[] { 60621 };
    
            foreach (var a in array1)
            {
                foreach (var b in array2)
                {
                    foreach (var c in array3)
                    {
                        Console.WriteLine("{0},{1},{2}", a, b, c);
                    }
                }
            }
    

    See if you can adapt that for N cases.

提交回复
热议问题