题目链接:UVa 11817 Tunnelling the Earth
几何题,求球面距离。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double PI = acos(-1);
const double R = 6371009;
//角度转化为弧度
double torad(double deg)
{
return deg / 180 * PI;
}
//经纬度(角度)转化为空间坐标
// lat 纬度
// lng 经度
void get_coord(double R, 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);
}
//x1,x2是纬度
//y1,y2是经度
double dis(double R, double x1, double y1, double x2, double y2)
{
double a1, b1, a2, b2, c1, c2;
get_coord(R, x1, y1, a1, b1, c1);
get_coord(R, x2, y2, a2, b2, c2);
return sqrt((a1 - a2) * (a1 - a2) + (b1 - b2) * (b1 - b2) + (c1 - c2) * (c1 - c2));
}
int main()
{
int T;
scanf("%d", &T);
double x1, y1, x2, y2;
while(T--)
{
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
double d1, d2;
d1 = dis(R, x1, y1, x2, y2);
d2 = 2 * R * asin(d1 / (2 * R));
int dif = (int)(d2 - d1 + 0.5);
printf("%d\n", dif);
}
return 0;
}
本文介绍了一个解决UVa11817问题的算法,即如何通过经纬度计算地球上的两点之间的球面距离。通过将问题转化为三维空间坐标,使用三角函数计算距离,并提供了详细的实现代码。
2272

被折叠的 条评论
为什么被折叠?



