计蒜客A1484 Tunnelling the Earth

There are different methods of transporting people from place to place: cars, bikes, boats, trains, planes, etc. For very long distances, people generally fly in a plane. But this has the disadvantage that the plane must fly around the curved surface of the earth. A distance travelled would be shorter if the traveller followed a straight line from one point to the other through a tunnel through the earth.

For example, travelling from Waterloo to Cairo requires a distance of 92935219293521 metres following the great circle route around the earth, but only 84911888491188 metres following the straight line through the earth.

For this problem, assume that the earth is a perfect sphere with radius of 63710096371009 metres.

Input Specification
The first line of input contains a single integer, the number of test cases to follow. Each test case is one line containing four floating point numbers: the latitude and longitude of the origin of the trip, followed by the latitude and longitude of the destination of the trip. All of these measurements are in degrees. Positive numbers indicate North latitude and East longitude, while negative numbers indicate South latitude and West longitude.

Output Specification
For each test case, output a line containing a single integer, the difference in the distance between the two points following the great circle route around the surface of the earth and following the straight line through the earth, in metres. Round the difference of the distances to the nearest integer number of metres.

样例输入复制
1
43.466667 -80.516667 30.058056 31.228889
样例输出复制
802333
题目来源
10 July, 2010 - Waterloo local contest

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

const long long R = 6371009;
const double Pai = acos(-1);

inline double Midify(double x) { return x * Pai / 180.0; }

inline double calc1(double x) {
    // 我用的是解三角形公式    其实网上有一种根据两点经纬度计算两点之间球面距离的公式  但是比较那啥...  我也没看懂
    return R * acos((R * R * 2 - x * x ) / (2 * R * R)
            );
}

inline double calc2(double x,double y,double a,double b) {
    // 拆解三维坐标  求两点之间的距离
    // 下午比赛的时候我居然傻*到企图拆解成二维的
    double x1 = R * cos(y) * cos(x),y1 = R * cos(y) * sin(x),z1 = R * sin(y);
    double x2 = R * cos(b) * cos(a),y2 = R * cos(b) * sin(a),z2 = R * sin(b);
    return pow(x1 - x2,2) + pow(y1 - y2,2) + pow(z1 - z2,2);
}

int main(int argc,char *argv[]) {
    int T; scanf("%d",&T);
    double x,y,a,b;
    while(T--) {
        scanf("%lf %lf %lf %lf",&y,&x,&b,&a); // y b --纬度
        x = Midify(x);
        y = Midify(y);
        a = Midify(a);
        b = Midify(b);
        double dist = sqrt( calc2(x,y,a,b) );
        printf("%d\n",int(calc1(dist) - dist + 0.5));
    }


    return 0;
}

附上网上的一种做法
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_35776409/article/details/107470569