http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2917

球坐标系转直角坐标系搞定。

/*0.019s*/

#include<bits/stdc++.h>
using namespace std;
const double R = 6371009.0;

inline double torad(double deg) {return deg / 180 * M_PI;}

void get_coord(double lat, double lng, double& x, double& y, double& z)
{
    lat = torad(lat);
    lng = torad(lng);
    x = R * cos(lat) * cos(lng);
    y = R * cos(lat) * sin(lng);
    z = R * sin(lat);
}

inline double sq(double x) {return x * x;}

double dist(double x1, double y1, double z1, double x2, double y2, double z2)
{
    return sqrt(sq(x1 - x2) + sq(y1 - y2) + sq(z1 - z2));
}

int main()
{
    int T;
    double lat1, lng1, lat2, lng2;
    double x1, y1, z1, x2, y2, z2;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%lf%lf%lf%lf", &lat1, &lng1, &lat2, &lng2);
        get_coord(lat1, lng1, x1, y1, z1);
        get_coord(lat2, lng2, x2, y2, z2);
        double d = dist(x1, y1, z1, x2, y2, z2);
        double d2 = 2 * R * asin(d / (2 * R));
        printf("%.0f\n", d2 - d);
    }
    return 0;
}

Comments

comments powered by Disqus