這題主要知道如何把經緯度轉化為三維點坐標即可
代碼:
#include#include #include #include using namespace std; const double PI = acos(-1.0); int t; double lat[2], lng[2]; //角度轉化為弧度 double torad(double deg) { return deg / 180 * PI; } struct Point3 { double x, y, z; Point3() {} Point3(double x, double y, double z) { this->x = x; this->y = y; this->z = z; } }; const double R = 6371009; Point3 get_coord(double R, double lat, double lng) { lat = torad(lat); lng = torad(lng); return Point3(R * cos(lat) * cos(lng), R * cos(lat) * sin(lng), R * sin(lat)); } double dist(Point3 p, Point3 q) { double dx = p.x - q.x; double dy = p.y - q.y; double dz = p.z - q.z; return sqrt(dx * dx + dy * dy + dz * dz); } int main() { scanf("%d", &t); while (t--) { scanf("%lf%lf%lf%lf", &lat[0], &lng[0], &lat[1], &lng[1]); Point3 p[2]; p[0] = get_coord(R, lat[0], lng[0]); p[1] = get_coord(R, lat[1], lng[1]); double d = dist(p[0], p[1]); printf("%.0f\n", 2 * asin(d / 2 / R) * R - d); } return 0; }