UVA-10221: Satellites

  1. 1. 內容
  2. 2. 範例輸入
  3. 3. 範例輸出
  4. 4. 想法
  5. 5. 程式碼

CPE 一顆星選集 49 道必考題

題目連結:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1162

內容


給定地球半徑 6440 km,地球表面到衛星距離 s ,夾角 a (可能為 min 或 deg),求出兩衛星的距離(弧長,直線距離)。

每筆測資一列,分別為 s、a、(min or deg)。

範例輸入

500 30 deg
700 60 min
200 45 deg

範例輸出

3633.775503 3592.408346
124.616509 124.614927
5215.043805 5082.035982

想法

deg = min / 60。
角度超過 180 要拿 360 去減。
pi = 2.0*acos(0.0)。
弧長 = 2*pi*R*角度/360。
直線距離可用正弦定理或餘弦定理求出(我不知道我的正弦出了什麼Bug,一直WA = =)。

sin(), cos() 傳入的是弧度。
最後用 setprecision() 控制小數點位數。

程式碼

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr)
using namespace std;

int main(){
fastio;
double dis, a, arc, chord;
string s;
double pi = 2.0*acos(0.0);
double r = 6440.0;
while(cin >> dis >> a >> s){
if(s == "min") a /= 60.0;
if(a > 180.0) a = 360.0 - a;
arc = 2.0*pi*(r+dis)*a/360.0;
double R = r+dis;
//chord = sin(a*pi/180.0)*(r+dis) / sin(((180.0-a)/2.0)*pi/180.0);
chord = sqrt(R*R + R*R - 2.0*R*R*cos(a*pi/180.0));
cout << fixed << setprecision(6) << arc << " " << chord << "\n";
}
return 0;
}