C# - 使用递归在Array中求和

时间:2017-05-24 01:40:55

标签: c# arrays visual-studio recursion

所以我现在正在尝试使用递归方法计算数组中所有行李的总和。我确定它很简单,但我似乎无法理解它。任何援助将不胜感激! - 干杯。

class Program
{
    static void Main(string[] args)
    {
        List<Bag> bags = new List<Bag>();
        bags.Add(new Bag("Blue", 25));
        bags.Add(new Bag("Red", 35));
        bags.Add(new Bag("White", 30));
        int totalVolume = CalcTotalVolume(bags);
        Console.WriteLine("Total volume of bags: {0}", totalVolume);
    }
    static int CalcTotalVolume(List<Bag> bagList)
    {
        //resursive method
        //1. base case is when the list is empty
        if (bagList.Count == 0)
        {
            return 0;
        }
        else
        {
            List<int> subList = bagList.GetRange(1, bagList.Volume - 1);
            int subTotal = CalcTotalVolume(subList);
            int total = bagList[1] + subTotal;
            return total;
        }
    }

}//end of class Program
class Bag
{

    public string Colour { get; set; }
    public int Volume { get; set; }
    public Bag(string co, int vo)
    {
        Colour = co;
        Volume = vo;
    }
}

2 个答案:

答案 0 :(得分:2)

显然循环效率要高得多,但对于kata来说,这很有意思......

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Bag> bags = new List<Bag>();
            bags.Add(new Bag("Blue", 25));
            bags.Add(new Bag("Red", 35));
            bags.Add(new Bag("White", 30));
            int totalVolume = CalcTotalVolume(bags);
            Console.WriteLine("Total volume of bags: {0}", totalVolume);
            Console.ReadKey(true);
        }

        static int CalcTotalVolume(IEnumerable<Bag> bags)
        {
            //resursive method
            //1. base case is when the list is empty
            var bag = bags.FirstOrDefault();
            if (bag == null) return 0;

            var subList = bags.Skip(1);
            return bag.Volume + CalcTotalVolume(subList);
        }
    }

    class Bag
    {

        public string Colour { get; set; }
        public int Volume { get; set; }
        public Bag(string co, int vo)
        {
            Colour = co;
            Volume = vo;
        }
    }

}

答案 1 :(得分:1)

知道你想要什么样的递归会很有趣。例如,以下也使用递归方法,但它相当于一个简单的求和循环:

class Bag
{
    public string Colour { get; }
    public int Volume { get; }
    public Bag(string c, int v)
    {
        Colour = c;
        Volume = v;
    }
}

class Program
{
    static int CalcTotalVolumeIdx(List<Bag> bags, int i, int sum)
    {
        return (i >= bags.Count) ? sum :
            CalcTotalVolumeIdx(bags, i + 1, sum + bags[i].Volume);
    }

    static int CalcTotalVolume(List<Bag> bags)
    {
        return CalcTotalVolumeIdx(bags, 0, 0);
    }

    static void Main(string[] args)
    {
        List<Bag> bags = new List<Bag>();
        bags.Add(new Bag("Blue", 25));
        bags.Add(new Bag("Red", 35));
        bags.Add(new Bag("White", 30));
        int totalVolume = CalcTotalVolume(bags);
        Console.WriteLine("Total volume of bags: {0}", totalVolume);
    }
}

作为旁注,F#实际上将函数CalcTotalVolumeIdx()编译为while循环,因为这种类型的递归是一种众所周知的模式,可以有效地转换。

编辑以反映布莱恩的评论。谢谢!