读取多个记录以计算点之间的最短距离时出错

时间:2014-03-13 14:18:21

标签: c# jquery sql algorithm

我试图编写这个伪代码,但结果不正确。 这是伪代码:

total_distance = distance(student(1), school)
For i = 1 to m-1 do (m is the number of students in the cluster)
    Total_distance = total_distance + distance(student(i), student(i+1))
End for

这是我的代码:

1 个答案:

答案 0 :(得分:1)

据我所知,你试着读两次学生的坐标: 首先,当你找到它与之前学生之间的距离时,第二,当你在tempLong和tempLat中保存它的坐标时。我猜你的输入不包含每个学生两次。 为避免两次读取每个值,您可以存储以前的值,如下所示:

    double curLong = 0.0;
    double curLat = 0.0;
    double prevLong = (double)schoolLocLon;
    double prevLat = (double)schoolLocLat;
    double totalDistance = 0.0;
    while (readStd.Read())
    {
        curLong = Convert.ToDouble(readStd.GetValue(0));
        curLat = Convert.ToDouble(readStd.GetValue(1));
        totalDistance += DistanceBetweenPlaces(prevLong, prevLat, curLong, curLat);
        prevLong = curLong;
        prevLat = curLat;
    }