UVa 11817 Tunnelling the Earth(球面距离)

本文介绍了一个解决UVa11817问题的算法,即如何通过经纬度计算地球上的两点之间的球面距离。通过将问题转化为三维空间坐标,使用三角函数计算距离,并提供了详细的实现代码。

题目链接: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;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值