解决方案 »
- 用VS写了两个Silverlight的工程,怎样使两个工程互相访问?
- 关键javascript 中变量的连接 , 在线等!!!
- 选中dataGridView一条记录,但是相应的combox值没有发生改变,斑竹在吗?急啊.........在线等答案.........
- sp1234,大家来找茬,我这个是面向对象还是基于对象?
- OleDbDataReader的关闭的问题
- 我想用c#三层结构的oa换c#三层结构的全站程序学习。
- 为什么我用VS.NET C#写的软件只能在windows2003上运行?
- 微软有没有打算在Linux下出FrameWork?
- 如何得到IE中设置的代理服务器的地址和端口?
- 请问,在C#菜单中怎么样为菜单添加图标?
- c# winform MonthCalendar控件能否不让编辑年月?
- C#自动填表的下拉框问题
1.前后2条记录相差小于100 即 (A-B)绝对值< 100 例如:A:30 B:150 C:40 那么忽略掉B
2.后一条数据必须比前一条数据大 例如:A:30 B:20 C:40 则忽略B 现提供如下数组做测试:
double[] array = new Double[]
{
21.1,
23.3,
24.4,
25.5,
26.6,
20.1,
26.8,
27.2,
27.4,
0,
28,
29,
30,
150,
31
};所需过滤掉的数据:20.1、0、150
double first = newArray[0];
for (int i = 1; i < newArray.Count;)
{
if (newArray[i] > first && newArray[i] - first < 100)
first = newArray[i++];
else
newArray.Remove(newArray[i]);
}
嗯 很对 是这个意思。只是如果用 Linq 怎么写?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double[] array = new Double[]
{
21.1,
23.3,
24.4,
25.5,
26.6,
20.1,
26.8,
27.2,
27.4,
0,
28,
29,
30,
150,
31
};
var query = array.Select((x, i) => new { x, i }).Zip(array.Skip(1), (f, s) => new { f = f.x, s, i = f.i + 1 }).Where(x => x.f > x.s || x.f < x.s - 100);
foreach (var item in query)
{
Console.WriteLine("index {0} value {1}", item.i, item.s);
}
}
}
}index 5 value 20.1
index 9 value 0
index 13 value 150
index 14 value 31
Press any key to continue . . .
array.Aggregate((x,y)=>
{
if(y>x && y-x<100) {result.Add(y); return y;}
else return x;
});
double[] array = new Double[]
{
24
23.3,
24.4,
25.5,
26.6,
20.1,
26.8,
27.2,
27.4,
0,
28,
29,
30,
150,
31
};
那么 以上方法貌似都不行了,其实 总结规则就是:排除异常增大的点。
{
var array = new Double[] { 21.1, 23.3, 24.4, 25.5, 26.6, 20.1, 26.8, 27.2, 27.4, 0, 28, 29, 30, 150, 31 }.ToList();
List<double> result = new List<double>();
int index;
var first = GetFirst(array, out index);
if (first == -1)
{
Console.WriteLine("给定集合中没有复合条件的元素");
return;
} if (index > 0)
{
array.RemoveRange(0, index - 1);
} result.Add(first);
var temp = (from item in array
select Check(item, result)).ToList();
Console.WriteLine(string.Join(", ", result));
} /// <summary>
/// 根据给定规则检查
/// </summary>
/// <param name="para"></param>
/// <param name="result"></param>
/// <returns></returns>
static double Check(double para, List<double> result)
{
var last = result[result.Count - 1];
if (para > last && (para - last < 100)) result.Add(para);
return para;
} /// <summary>
/// 寻找第一个满足条件的数
/// </summary>
/// <param name="source"></param>
/// <param name="index"></param>
/// <returns></returns>
static double GetFirst(List<double> source, out int index)
{
var count = source.Count();
index = 0;
for (int i = 0; i < count; i++)
{
if (++index > count) return -1;
if (source[i] < source[index] && (source[index] - source[i] < 100))
{
index = i;
break;
} if (index == count) return -1;
} return source[index];
}下面的for循环都可以转换为linq查询,时间关系,这里就不在赘述了。这里我觉得楼主是需要一个在linq里调用方法的方法吧