Problem:
给定球的半径,两点的经纬度,求这两点间的最短距离。
Solution:
球面两点间距离公式:
r*acos(cos(wa)*cos(wb)*cos(jb-ja) + sin(wa)*sin(wb))
r代表半径,wa是a点的纬度,wb是b点的纬度,ja是a点的经度,jb是b点的经度,去北纬为正,东经为正。
note:
cout输入输出有一定的技巧需要掌握。
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
const double PI = 3.141592653589;
//degree to its radian number
double convert(double degree) {
return degree*PI/180;
}
//parameters are radian number
double calculate(double r, double wa, double ja, double wb, double jb) {
return r*acos(cos(wa)*cos(wb)*cos(jb-ja) + sin(wa)*sin(wb));
}
int main() {
double r, waa, wbb, jaa, jbb;
char c;
while(cin >> r) {
if(r == 0)
break;
cin >> c >> waa;
if(c == 'S')
waa = -waa;
cin >> c >> jaa;
if(c == 'W')
jaa = -jaa;
cin >> c >> wbb;
if(c == 'S')
wbb = -wbb;
cin >> c >> jbb;
if(c == 'W')
jbb = -jbb;
cout << fixed << setprecision(2) << calculate(r, convert(waa), convert(jaa), convert(wbb), convert(jbb)) << endl;
}
return 0;
}