编程题:Titanic(地球上两地距离)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了编程题:Titanic(地球上两地距离),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3027字,纯文字阅读大概需要5分钟。
内容图文
![编程题:Titanic(地球上两地距离)](/upload/InfoBanner/zyjiaocheng/610/32dd6a1daa464a3c94db5a8bb4825261.jpg)
It is a historical fact that during the legendary voyage of “Titanic” the wireless telegraph machine had delivered 6 warnings about the danger of icebergs. Each of the telegraph messages described the point where an iceberg had been noticed. The first five warnings were transferred to the captain of the ship. The sixth one came late at night and a telegraph operator did not notice that the coordinates mentioned were very close to the current ship’s position.
Write a program that will warn the operator about the danger of icebergs!
The input messages are of the following format:
Message #<n>.
Received at <HH>:<MM>:<SS>.
Current ship’s coordinates are
<X1>^<X2>’<X3>" <NL/SL>
and <Y1>^<Y2>’<Y3>" <EL/WL>.
An iceberg was noticed at
^’" <NL/SL>
and ^’" <EL/WL>.
===
Here is a positive integer, :: is the time of the
message reception, <X1>^<X2>’<X3>" <NL/SL> and <Y1>^<Y2>’<Y3>" <EL/WL>
means “X1 degrees X2 minutes X3 seconds of North (South) latitude and
Y1 degrees Y2 minutes Y3 seconds of East (West) longitude.”
Output
Your program should print to the output file message in the following format:
The distance to the iceberg: <s> miles.
Where <s> should be the distance between the ship and the iceberg,
(that is the length of the shortest path on the sphere between the
ship and the iceberg). This distance should be printed up to (and
correct to) two decimal digits. If this distance is less than (but not
equal to!) 100 miles the program should print one more line with the
text:
DANGER!
Sample Input
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.
===
Sample Output
The distance to the iceberg: 52.04 miles.
DANGER!
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
using namespace std;
double pi=acos(-1.0); // acos()函数不接受 int型,否则将编译错误
double R=3437.5; //地球半径(英里)
inline void change(double &x,double &y){
x=x*pi/180;
y=y*pi/180;
}
//A(wa,jb),B(wx,jy),w 表示维度; j 表示经度。北纬、东经取正
void distance(double a,double b,double x,double y){
double d;
//为计算地球两点的距离公式,请自行推导
d=R* acos(cos(a)*cos(x)*cos(b-y)+sin(a)*sin(x));
printf("The distance to the iceberg: %.2lf miles.\n",d);
if(floor(d+0.005)<100)
cout<<"DANGER!\n";
}
int main(){
string s;
double a1,a2,a3,b1,b2,b3,shipw,shipj,icebergw,icebergj;
char a,b;
int i;
for(i=0;i<3;i++)
getline(cin,s);
scanf("%lf^%lf'%lf\" %cL ",&a1,&a2,&a3,&a);
scanf("and %lf^%lf'%lf\" %cL.",&b1,&b2,&b3,&b);
shipw=a1+ a2/60+ a3/3600;
shipj=b1+ b2/60+ b3/3600;
if(a=='S') shipw*=-1;
if(b=='W') shipj*=-1;
change(shipw,shipj);
getchar(); //当 getline函数 前使用过 scanf函数 需要getchar()读取未被scanf读到的 \n
getline(cin,s);
scanf("%lf^%lf'%lf\" %cL ",&a1,&a2,&a3,&a);
scanf("and %lf^%lf'%lf\" %cL.",&b1,&b2,&b3,&b);
icebergw=a1+ a2/60 +a3/3600;
icebergj=b1+ b2/60 +b3/3600;
if(a=='S') icebergw*=-1;
if(b=='W') icebergj*=-1;
change(icebergw,icebergj);
getchar();
getline(cin,s);
distance(shipw,shipj,icebergw,icebergj);
}
内容总结
以上是互联网集市为您收集整理的编程题:Titanic(地球上两地距离)全部内容,希望文章能够帮你解决编程题:Titanic(地球上两地距离)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。