第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用遞歸查找二維數(shù)組第一行和最后一行中所有值的總和

使用遞歸查找二維數(shù)組第一行和最后一行中所有值的總和

C#
溫溫醬 2021-07-06 13:34:38
我正在 c# 中練習(xí)遞歸,我有一個有效的解決方案,但它使用了 3 個函數(shù)(其中 2 個非常相似)。我正在尋找有關(guān)如何改進這一點的提示,或者我是否以正確的方式進行處理。我避免使用 for 循環(huán)并希望只使用遞歸來解決這個問題。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();        }    }}
查看完整描述

3 回答

?
不負相思意

TA貢獻1777條經(jīng)驗 獲得超10個贊

我知道您正在嘗試使用遞歸,但是使用 LINQ 進行此練習(xí)要簡單得多。


如果你從這個開始:


int[,] twoDArr = new int[,]

{

    { 1, 2, 3 },

    { 2, 3, 4 },

    { 3, 4, 5 },

};

int[][]通過這樣做可以很容易地將其轉(zhuǎn)換為 a :


int[][] rows =

    twoDArr

        .Cast<int>() // flattens to one dimension

        .Select((value, index) => new { value, index })

        .GroupBy(x => x.index / twoDArr.GetLength(1), x => x.value)

        .Select(x => x.ToArray())

        .ToArray();

在.GroupBy關(guān)鍵的是x.index / twoDArr.GetLongLength(1),這樣的從零開始的每一行的整數(shù)。


這給出了:

http://img1.sycdn.imooc.com//60eab5f0000104d501510286.jpg

現(xiàn)在你可以簡單地這樣做:

int result = rows.First().Sum() + rows.Last().Sum();

我從我的樣本數(shù)據(jù)中得到的結(jié)果是18(這是第一行和最后一行的正確總和)。


查看完整回答
反對 回復(fù) 2021-07-11
?
慕姐4208626

TA貢獻1852條經(jīng)驗 獲得超7個贊

這與您的代碼無關(guān)。這只是我對第一行和最后一行進行遞歸求和的版本。我希望它有幫助。


int SumFirstLastRows(int[,] twoD)

{

    // we need some pointer information on when to start and end,

    // let's use column and row number, starting from 0,0

    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' numbers

int SumRecursive(int[,] twoD, int column, int row)

{

    // determine the max row and column, to know when to end

    int maxRows = twoD.GetLength(0);

    int maxColumns= twoD.GetLength(1);


    if (row == maxRows)

    {

        // we are at the end of the rows, end everything

        return 0;

    }

    else if (column == maxColumns)

    {

        // we are at the end of column, switch to the next row instead

        return SumRecursive(twoD, 0, row + 1);

    }

    if ((row== 0 || row == maxRows-1) && column < maxColumns)

    {

        // only for the first or last row: current number + next column sum

        return twoD[row, column] + SumRecursive(twoD, column + 1, row);

    }

    else if(column < maxColumns)

    {

        // not the first or last row, so just skip to the next column

        return SumRecursive(twoD, column + 1, row);

    }

    return 0;

}

測試:


int[,] twod = new int[3,4]

        { {1,2,3,4 },

          {5,6,7,8 },

          {9,10,11,12 }

        };

int recursiveTest = SumFirstLastRows(twod);

int forVerification = 1 + 2 + 3 + 4 + 9 + 10 + 11 + 12;

bool isThisCorrect = recursiveTest == forVerification; // return true!


查看完整回答
反對 回復(fù) 2021-07-11
  • 3 回答
  • 0 關(guān)注
  • 272 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號