POJ_2354
这个题目本质就是应用一个求球面上两点间球面距离的公式,这个可以在网上搜到。但这个题输入比较恶心,而且最后和100比较的时候先要把算出来的结果四舍五入到百分位,然后再和100去比较。
#include<stdio.h>
#include<string.h>
#include<math.h>
#define zero 1e-8
const double R = 6875.0 / 2;
const double pi = acos(-1.0);
double a1, a2, b1, b2;
char b[110], st[10];
int dcmp(double x)
{
return fabs(x) < zero ? 0 : (x < 0 ? -1 : 1);
}
void init()
{
int i, j, k, t1, t2, t3;
for(i = 0; i < 8; i ++)
scanf("%s", b);
scanf("%s", b);
sscanf(b, "%d^%d'%d''", &t1, &t2, &t3);
b1 = t1 + (double)t2 / 60 + (double)t3 / 3600;
scanf("%s", st);
if(st[0] == 'S')
b1 = -b1;
scanf("%s", b);
scanf("%s", b);
sscanf(b, "%d^%d'%d''", &t1, &t2, &t3);
a1 = t1 + (double)t2 / 60 + (double)t3 / 3600;
scanf("%s", st);
if(st[0] == 'W')
a1 = -a1;
for(i = 0; i < 5; i ++)
scanf("%s", b);
scanf("%s", b);
sscanf(b, "%d^%d'%d''", &t1, &t2, &t3);
b2 = t1 + (double)t2 / 60 + (double)t3 / 3600;
scanf("%s", st);
if(st[0] == 'S')
b2 = -b2;
scanf("%s", b);
scanf("%s", b);
sscanf(b, "%d^%d'%d''", &t1, &t2, &t3);
a2 = t1 + (double)t2 / 60 + (double)t3 / 3600;
scanf("%s", st);
if(st[0] == 'W')
a2 = -a2;
scanf("%s", b);
}
void solve()
{
double theta, ans;
a1 = a1 * pi / 180, b1 = b1 * pi / 180, a2 = a2 * pi / 180, b2 = b2 * pi / 180;
theta = acos(cos(b1) * cos(b2) * cos(a1 - a2) + sin(b1) * sin(b2));
ans = theta * R;
printf("The distance to the iceberg: %.2lf miles.\n", ans);
if(dcmp(ans - 100 + 0.005) < 0)
printf("DANGER!\n");
}
int main()
{
while(scanf("%s", b) == 1)
{
init();
solve();
}
return 0;
}