poj 2354

题目概述

给定地球上两点的经纬度,求出两点间球面距离
设地球为直径6875的标准球体

时限

1000ms/3000ms

输入

输入共九行,第四行为点1的纬度(角度,分,秒)及南北纬,第五行为点1的经度及东西经,第七,八行为点2的,输入只有一组

限制

没有限制

输出

第一行为字符串
The distance to the iceberg: # miles.
其中#为保留两位小数的浮点数,为所求距离,若#<100,则在第二行输出
DANGER!

样例输入

Message #513.
Received at 22:30:11.
Current ship’s coordinates are
41^46’00” NL
and 50^14’00” WL.
An iceberg was noticed at
41^14’11” NL
and 51^09’00” WL.
===

样例输出

The distance to the iceberg: 52.04 miles.
DANGER!

讨论

计算几何,解析几何,给出经纬度算距离是有公式的,按照题解代码中的变量如下

dis=rarccos(cos(y1)cos(y2)cos(x2x1)+sin(y1)sin(y2))
其中y是纬度,x是经度,r是地球半径
这个公式具体的推导过程可以参考
http://www.360doc.com/content/14/0117/10/325430_345890919.shtml
计算并不复杂,但是要写成一个公式确实需要不少化简
实现方面,这个题由于最后的DANGER!所以对精度有点奇怪的要求,先+0.005消除舍入误差,然后-100和0.0001比较,不这样处理的基本都会错

题解状态

212K,0MS,C++,1124B

题解代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 103
#define memset0(a) memset(a,0,sizeof(a))
#define EPS 1e-8

char s[20];//string 存放没用的输入的字符串
const double r = 6875 / 2.0, pi = atan(1.0) * 4;//地球半径 题目给的是直径 pi 角度转弧度用
void fun()
{
    int a, b, c;
    scanf("%s%s%s%s%s%s%s%s%s%d^%d\'%d\"%s", s, s, s, s, s, s, s, s, s, &a, &b, &c, s);//input
    double y1 = (a + b / 60.0 + c / 3600.0)*(s[0] == 'N' ? 1 : -1) / 180 * pi;//角度 分 秒的转换都是60进制
    scanf("%s%d^%d'%d\"%s", s, &a, &b, &c, s);//input
    double x1 = (a + b / 60.0 + c / 3600.0)*(s[0] == 'E' ? 1 : -1) / 180 * pi;
    scanf("%s%s%s%s%s%d^%d'%d\"%s", s, s, s, s, s, &a, &b, &c, s);//input
    double y2 = (a + b / 60.0 + c / 3600.0)*(s[0] == 'N' ? 1 : -1) / 180 * pi;
    scanf("%s%d^%d'%d\"%s", s, &a, &b, &c, s);//input
    double x2 = (a + b / 60.0 + c / 3600.0)*(s[0] == 'E' ? 1 : -1) / 180 * pi;
    double dis = r*acos(cos(y1)*cos(y2)*cos(x2 - x1) + sin(y1)*sin(y2));
    scanf("%s", s);//input
    printf("The distance to the iceberg: %.2lf miles.\n%s", dis, dis + 0.005 - 100 < 0.0001 ? "DANGER!\n" : "");//output
}
int main(void)
{
    //freopen("vs_cin.txt", "r", stdin);
    //freopen("vs_cout.txt", "w", stdout);

    fun();
}

EOF

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值