…… int j =0; for (int i = 2; i < data.Count; i++) { if (data[i] == data[i - 1] + 1) { j++; } else { j=0; } if(j == 9) { return ture; } } return false; …… 新手,有误请指教。
private bool Test(List<long> phongs) { List<long> data = phongs.OrderBy(x => x).ToList(); for (int i = 0; i < data.Count()-10; i++) { if(data[i+9]-data[i]==10) return true; } return true; }
LS好方法。。 就是最后false写成true了……
IF 错了,应该是data[i+9] - data[i]==9, 似乎是,随手写的,没调试
CS1061: “System.Collections.Generic.List<long>”不包含“OrderBy”的定义,并且找不到可接受类型为“System.Collections.Generic.List<long>”的第一个参数的扩展方法“OrderBy”(是否缺少 using 指令或程序集引用?) 有报错?
那就掉用Sort方法, ORDERBY是3.5的扩展方法
你如果LIST已经是排好序的话就不用ORDERBY了。
private bool Test(List<long> phongs) { phongs.Sort(); for (int i = 0; i < phongs.Count - 10; i++) { if (phongs[i + 9] - phongs[i] == 9) { return true; } } return false; }
……
int j =0;
for (int i = 2; i < data.Count; i++)
{
if (data[i] == data[i - 1] + 1)
{
j++;
}
else
{
j=0;
}
if(j == 9)
{
return ture;
}
}
return false;
……
新手,有误请指教。
{
List<long> data = phongs.OrderBy(x => x).ToList();
for (int i = 0; i < data.Count()-10; i++)
{
if(data[i+9]-data[i]==10)
return true;
}
return true;
}
就是最后false写成true了……
CS1061: “System.Collections.Generic.List<long>”不包含“OrderBy”的定义,并且找不到可接受类型为“System.Collections.Generic.List<long>”的第一个参数的扩展方法“OrderBy”(是否缺少 using 指令或程序集引用?)
有报错?
{
phongs.Sort();
for (int i = 0; i < phongs.Count - 10; i++)
{
if (phongs[i + 9] - phongs[i] == 9)
{
return true;
}
}
return false;
}
private bool Test(List<long> phongs,int n) {
//排序,可以用sortfor (int i = 2; i < phongs.Count(); i=i+n/2)
{
for(j=1;j<n;j++)
{
判定
}
}
}
{
//我就没有写排序的了,需要先排序(自增),然后 运行下面
List<int> phones = new List<int>();
for (int i = 0; i < 20; i++)
{
phones.Add(1377 + i); //保证自增
} phones.Remove(1377);
phones.Remove(1388); List<int> result = new List<int>(); for (int i = phones.Count-1; i >0; i--)
{ if (phones[i] == phones[i - 1]+1)
{
result.Add(phones[i]);
}
else if(phones[i] != phones[i - 1]+1&&result.Count < 9)
{
result.Clear();
}
}
if (result.Count == 9)
{
result.Add(phones[0]);
Console.Write("10 Continuous"); //对应于 return ture
Console.ReadLine();
}
}