使用递归查找二维数组第一行和最后一行中所有值的总和

我正在 c# 中练习递归,我有一个有效的解决方案,但它使用了 3 个函数(其中 2 个非常相似)。我正在寻找有关如何改进这一点的提示,或者我是否以正确的方式进行处理。我避免使用 for 循环并希望只使用递归来解决这个问题。


using System;


namespace RecursionPractice

{

    class Program

    {

        static int sumFirstLastRows(int[,] twoDArr)

        {

            int rowLength = twoDArr.GetLength(1); 


            int sumRow1 = sumFirstRow(twoDArr, rowLength);

            int sumRow2 = sumLastRow(twoDArr, rowLength);


            int sumTotal = sumRow1 + sumRow2;

            return sumTotal;

        }


        static int sumFirstRow(int[,] twoDArr, int N)

        {

            if (N <= 0)

            {

                //base case

                return 0;

            }


            return sumFirstRow(twoDArr, N - 1) + twoDArr[0, N - 1];

        }


        static int sumLastRow(int[,] twoDArr, int N)

        {

            if (N <= 0)

            {

                //base case

                return 0;

            }


            return sumLastRow(twoDArr, N - 1) + twoDArr[1, N - 1];

        }



        static void Main(string[] args)

        {

            int[,] twoD = new int[,] {{ 1, 3, 5 },

                                     {  2, 4, 6  }};


            Console.WriteLine(sumFirstLastRows(twoD));


            Console.ReadLine();

        }

    }

}


温温酱
浏览 245回答 3
3回答

不负相思意

我知道您正在尝试使用递归,但是使用 LINQ 进行此练习要简单得多。如果你从这个开始:int[,] twoDArr = new int[,]{&nbsp; &nbsp; { 1, 2, 3 },&nbsp; &nbsp; { 2, 3, 4 },&nbsp; &nbsp; { 3, 4, 5 },};int[][]通过这样做可以很容易地将其转换为 a :int[][] rows =&nbsp; &nbsp; twoDArr&nbsp; &nbsp; &nbsp; &nbsp; .Cast<int>() // flattens to one dimension&nbsp; &nbsp; &nbsp; &nbsp; .Select((value, index) => new { value, index })&nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(x => x.index / twoDArr.GetLength(1), x => x.value)&nbsp; &nbsp; &nbsp; &nbsp; .Select(x => x.ToArray())&nbsp; &nbsp; &nbsp; &nbsp; .ToArray();在.GroupBy关键的是x.index / twoDArr.GetLongLength(1),这样的从零开始的每一行的整数。这给出了:现在你可以简单地这样做:int&nbsp;result&nbsp;=&nbsp;rows.First().Sum()&nbsp;+&nbsp;rows.Last().Sum();我从我的样本数据中得到的结果是18(这是第一行和最后一行的正确总和)。

慕姐4208626

这与您的代码无关。这只是我对第一行和最后一行进行递归求和的版本。我希望它有帮助。int SumFirstLastRows(int[,] twoD){&nbsp; &nbsp; // we need some pointer information on when to start and end,&nbsp; &nbsp; // let's use column and row number, starting from 0,0&nbsp; &nbsp; return SumRecursive(twoD, 0, 0);}// this is a recursive method, which goes for each row and column recursively,// however it only takes the sum of first and last rows' numbersint SumRecursive(int[,] twoD, int column, int row){&nbsp; &nbsp; // determine the max row and column, to know when to end&nbsp; &nbsp; int maxRows = twoD.GetLength(0);&nbsp; &nbsp; int maxColumns= twoD.GetLength(1);&nbsp; &nbsp; if (row == maxRows)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // we are at the end of the rows, end everything&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }&nbsp; &nbsp; else if (column == maxColumns)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // we are at the end of column, switch to the next row instead&nbsp; &nbsp; &nbsp; &nbsp; return SumRecursive(twoD, 0, row + 1);&nbsp; &nbsp; }&nbsp; &nbsp; if ((row== 0 || row == maxRows-1) && column < maxColumns)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // only for the first or last row: current number + next column sum&nbsp; &nbsp; &nbsp; &nbsp; return twoD[row, column] + SumRecursive(twoD, column + 1, row);&nbsp; &nbsp; }&nbsp; &nbsp; else if(column < maxColumns)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // not the first or last row, so just skip to the next column&nbsp; &nbsp; &nbsp; &nbsp; return SumRecursive(twoD, column + 1, row);&nbsp; &nbsp; }&nbsp; &nbsp; return 0;}测试:int[,] twod = new int[3,4]&nbsp; &nbsp; &nbsp; &nbsp; { {1,2,3,4 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {5,6,7,8 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {9,10,11,12 }&nbsp; &nbsp; &nbsp; &nbsp; };int recursiveTest = SumFirstLastRows(twod);int forVerification = 1 + 2 + 3 + 4 + 9 + 10 + 11 + 12;bool isThisCorrect = recursiveTest == forVerification; // return true!
打开App,查看更多内容
随时随地看视频慕课网APP