UVA 11817 Tunnelling the Earth

本文介绍了一种计算两点间沿地球表面行走与直线穿越地球内部距离之差的方法。使用C++编程实现,通过经纬度坐标转换为三维坐标,进而计算两点间球面距离及直线距离,并给出具体代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

题意:

给起点经纬度和终点经纬度

求沿地球表面走和钻隧道走直线的距离差

 

 

#include <vector>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps=1e-10;//精度
const int INF=1<<29;
const double PI=acos(-1);
int dcmp(double x){//判断double等于0或。。。
	if(fabs(x)<eps)return 0;else return x<0?-1:1;
}
struct Point{
	double x,y,z;
	Point(double x=0,double y=0,double z=0):x(x),y(y),z(z){}
};
typedef Point Vector;
Vector operator+(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y,a.z+b.z);}
Vector operator-(Point a,Point b){return Vector(a.x-b.x,a.y-b.y,a.z-b.z);}
Vector operator*(Vector a,double p){return Vector(a.x*p,a.y*p,a.z*p);}
Vector operator/(Vector a,double p){return Vector(a.x/p,a.y/p,a.z/p);}
bool operator==(Point a,Point b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0&&dcmp(a.z-b.z)==0;}
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y+a.z*b.z;}
double Length(Vector a){return sqrt(Dot(a,a));}
double Angle(Vector a,Vector b){return acos(Dot(a,b)/Length(a)/Length(b));}
double torad(double deg){return deg/180*PI;}
void getcoord(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);
}

const double r=6371009;
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		double lata,lnga,latb,lngb;
		scanf("%lf%lf%lf%lf",&lata,&lnga,&latb,&lngb);
		Point a,b;
		getcoord(r,lata,lnga,a.x,a.y,a.z);
		getcoord(r,latb,lngb,b.x,b.y,b.z);
		double dis=Length(a-b)/2;
		double angle=asin(dis/r);
		double ans=r*angle-dis;
		printf("%.0lf\n",ans*2);
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值