日期:2014-05-18 浏览次数:20959 次
var vs = num.Select((i, index) => new { i, inum = index-1 }).Where(n=>n.i==0).Select(m=>new {value=num[m.inum]});
------解决方案--------------------
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]);
------解决方案--------------------
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);