日期:2014-05-18  浏览次数:20911 次

如何计算这道与泛型有关的题,谢谢
List<int> num = new List<int> { 7, 0, 1, 2, 3, 4, 5, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 0, 1, 0, 1, 2, 3, 4, 5, 6 };

泛型 num 

1.如何求得,两个0前一个数值的和。即:7+5=12、5+0=5、 0+9=9、 9+2=11、2+1=3
2.在所得到的和值里{ 12、5、9、11、3 },求得最大值 12

------解决方案--------------------
C# code

var vs = num.Select((i, index) => new { i, inum = index-1 }).Where(n=>n.i==0).Select(m=>new {value=num[m.inum]});

------解决方案--------------------
C# code
            List<int> num = new List<int> { 7, 0, 1, 2, 3, 4, 5, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 0, 1, 0, 1, 2, 3, 4, 5, 6 };
            List<int> result = new List<int>();
            var q = num.Select((i, index) => new { i, index })
                       .Where(x => x.i == 0 && x.index > 0)
                       .Select(x => num[x.index - 1])
                       .Aggregate((x, y) => { result.Add(x + y); return y; });
            result.Sort(new Comparison<int>((x, y) => { return y - x; }));
            Console.WriteLine(result[0]);

------解决方案--------------------
C# code

List<int> num = new List<int> { 7, 0, 1, 2, 3, 4, 5, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 0, 1, 0, 1, 2, 3, 4, 5, 6 };
num = num.Where((x, index) => index < num.Count - 1 && num[index + 1] == 0).ToList();
int max = num.Select((x, index) => index < num.Count - 1 ? (x + num[index + 1]) : 0).Max();
Console.WriteLine(max);