日期:2014-05-18 浏览次数:20808 次
List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 }; List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 }; bool flag1 = CheckList(list1,3);//false bool flag2 = CheckList(list2, 3);//true
------解决方案--------------------
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace wanlinq { class Program { static void Main(string[] args) { List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 }; List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 }; Console.WriteLine(panduan(list1)); Console.WriteLine(panduan(list2)); Console.ReadLine(); } static bool panduan(List<int> myinput) { List<List<int>> re = new List<List<int>>(); //默认list里面至少有3个元素 for (int i = 0; i < myinput.Count - 2; i++) { List<int> re1 = new List<int>(); re1.Add(myinput[i]); re1.Add(myinput[i + 1]); re1.Add(myinput[i + 2]); re.Add(re1); } return re.Any(x => { if (x[0] + 1 == x[1] && x[0] + 2 == x[2]) { return true; } return false; } ); } } }
------解决方案--------------------
static bool getRes (IEnumerable<int> list) { if (list==null) { return false; } return Enumerable.Range(1, list.Count() - 2).Any(index => { int first = list.ElementAt(index - 1); int mid = list.ElementAt(index); int end = list.ElementAt(index + 1); return first < mid && mid < end && (mid * 2 == first + end); }); }
------解决方案--------------------
List<int> list1 = new List<int>() { 1, 2, 5, 12, 10, 4, 5 };
List<int> list2 = new List<int>() { 0, 1, 4, 2, 7, 8, 9 };
Console.WriteLine(Enumerable.Range(0, list2.Count() - 2).Any(index => list2[index] + list2[index + 2] == 2*list2[index + 1]));
Console.Read();
------解决方案--------------------
public static bool ListCheck(System.Collections.Generic.List<int> list)
{
return list.Where((t, index) => index > 0 && index < list.Count() - 1 && t * 2 == list[index - 1] + list[index + 1]).Count() > 0;
}
------解决方案--------------------