private bool Test(List<long> phongs) {
....
}phones 是一个手机号集合 判断在phones里面是否有手机10连号存在。不管10连号位置在哪,只要出现就返回false. 没有就返回true. 注意:是10连号 比如说15117172369,15117172370,15117172371就是一个3连号。
以前发帖求助过,不过这个算法都没人做出来能测试通过的。大家帮帮忙吧。谢谢。
....
}phones 是一个手机号集合 判断在phones里面是否有手机10连号存在。不管10连号位置在哪,只要出现就返回false. 没有就返回true. 注意:是10连号 比如说15117172369,15117172370,15117172371就是一个3连号。
以前发帖求助过,不过这个算法都没人做出来能测试通过的。大家帮帮忙吧。谢谢。
private bool Test(List<long> phongs)
{
var temp=new List<List<long>>();
phongs.OrderBy(p=>p).Aggregate((p1,p2)=>
{
if(temp.Count()==0)
{temp.Add(new List<long>{p1});}
else if(temp.Last().Last()+1==p1)
{
temp.Last().Add(p1);
// Console.WriteLine(p1);
}
else { temp.Add(new List<long>{p1});}
return p2;
});
return temp.Any(t=>t.Count()==10);
}
private void button1_Click(object sender, EventArgs e)
{
List<string> list = textBox1.Text.Split(',').ToList();
List<long> phones = new List<long>();
foreach (string s in list)
{
phones.Add(long.Parse(s));
}
MessageBox.Show(CheckPhone(phones).ToString());
}
private bool CheckPhone(List<long> phongs)
{
var temp = new List<List<long>>();
phongs.OrderBy(p => p).Aggregate((p1, p2) =>
{
if (temp.Count() == 0)
{ temp.Add(new List<long> { p1 }); }
else if (temp.Last().Last() + 1 == p1)
{
temp.Last().Add(p1);
}
else { temp.Add(new List<long> { p1 }); }
return p2;
});
return temp.Any(t => t.Count() == 10);
}
13993056368,13993056369,13993056370,13993056371,13993056372,13993056373,13993056374,13993056375,13993056376,13993056377,13993056372,13293056371,13993056383这组数返回的怎么是False呢?我第一个到第十个是连号呀。
4楼代码要修改一下:
phongs.Distinct().OrderBy(p=>p).Aggregate((p1,p2)=>
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<long> list1 = new List<long>()
{ 13802417000, 13802417017, 13802417018, 13802417019, 13802417020,
13802417021, 13802417022, 13802417023, 13802417024, 13802417025,
13802417026, 13802417027, 13802417028, 13802417029, 13802417030,
13802417023, 13802417041, 13802417042, 13802417043 };
List<long> list2 = new List<long>()
{ 13802417000, 13802417017, 13802417018, 13802417011, 13802417020,
13802417021, 13802417022, 13802417023, 13802417024, 13802417025,
13802417026, 13802417014, 13802417028, 13802417029, 13802417030,
13802417023, 13802417041, 13802417042, 13802417043 };
List<long> list3 = new List<long>()
{ 13993056368, 13993056369, 13993056370, 13993056371, 13993056372,
13993056373, 13993056374, 13993056375, 13993056376, 13993056377,
13993056372, 13293056371, 13993056383 };
Console.WriteLine(Test(list1));
Console.WriteLine(Test(list2));
Console.WriteLine(Test(list3));
}
static bool Test(List<long> phonenumbers)
{
var nums = phonenumbers.Distinct().OrderBy(x => x).ToList();
int count = 1;
for (int i = 1; i < nums.Count; i++)
{
if (nums[i] == nums[i - 1] + 1) count++; else count = 1;
if (count == 10) return false;
}
return true;
} }
}
你对调下。
是不是你写的???难怪这么简单的问题你老搞不定。
{
var nums = phonenumbers.Distinct().OrderBy(x => x).ToList();
int count = 1;
for (int i = 1; i < nums.Count; i++)
{
if (nums[i] == nums[i - 1] + 1) count++; else count = 1;
if (count == 10) return true;
}
return false;
}
你可以反过来。
class Program
{
static void Main(string[] args)
{
List<long> l = new List<long>() { 13802417000, 13802417017, 13802417018, 13802417019, 13802417020,
13802417021, 13802417022, 13802417023, 13802417024, 13802417025,
13802417026, 13802417027, 13802417028, 13802417029, 13802417030,
13802417023, 13802417041, 13802417042, 13802417043 };
Console.Write(Test(l).ToString());
} static bool Test(List<long> phones)
{
phones = phones.Distinct().OrderBy(o => o).ToList<long>();
int iCount = 1;
for (int i = 1; i < phones.Count(); i++)
{
if (phones[i] - phones[i - 1] == 1)
{
iCount++;
if (iCount >= 10) break;
}
else
{
iCount = 1;
}
}
return !(iCount >= 10);
}
}
如果是连位置也要保持连号的话.请Mark掉 phones = phones.Distinct().OrderBy(o => o).ToList<long>();
我的系统是用的2.0的框架,所有没有Distinct这个方法,有办法替代吗?