Recursion in place of multiple nested for loops?

前端 未结 5 1603
日久生厌
日久生厌 2021-01-14 02:05

Im having some issues with trying to update a nested for loop to use recursion instead. Is it possible to access the a,b and c variables from the earlier for loops when usin

5条回答
  •  孤街浪徒
    2021-01-14 03:10

    Off the top of my head, i.e. not tested, something like this might work:

        List collection = new List();
        private void AddValues(int a, int b, int c)
        {
    
            collection.Add(new[] { a, b, c });
    
            if (c < 10)
            {
                c++;
                AddValues(a, b, c);
            }
    
            if (b < 20)
            {
                b++;
                c = 0;
                AddValues(a, b, c);   
            }
    
            if (a < 10)
            {
                a++;
                b = 0;
                c = 0;
                AddValues(a, b, c);
            }
        }
    

    Start it by calling:

    AddValues(0, 0, 0);
    

提交回复
热议问题