有一些有趣的想法。假设我们有一个类似于以下表格的表格(在SQL Server中):
- 位置
- 速度
- 时间
例如:
Location Velocity Time
1 40 1:20
2 35 2:00
3 45 2:05
4 50 2:30
5 60 2:45
6 48 2:55
7 40 3:00
8 35 3:15
9 50 3:20
10 70 3:30
11 50 3:35
12 40 3:40
假设速度限制为40公里每小时,输出结果如下:
Starttime Endtime
2:05 3:00
3:20 3:35
什么是确定超速期间(定义了速度障碍)的最佳方法?我的第一个想法是将表加载到数组中,然后迭代数组以查找这些时间段:
(伪C#代码)
bool isOverSpeed = false;
for (int i =0;i<arr.Length;i++)
{
if (!isOverSpeed)
if (arr[i].Velocity > speedBarrier)
{
#insert the first record into another array.
isOverSpeed = true;
}
if(isOverSpeed)
if (arr[i].Velocity < speedBarrier)
{
#insert the record into that array
isOverSpeed = false;
}
}
它能够工作,但效果不是很理想。是否有更“智能”的方法,例如使用T-SQL查询或其他算法来完成这个任务?
Velocity > speedBarrier
的时间,作为TimeSpan
吗?这不太清楚。你期望的输出是什么? - Kobi