Topcoder SRM 721 Div.2 A. FlightDataRecorder

这场是我第一次打Topcoder,之前顶多只是写写前面的SRM的题。这一场在晚上11:00。我T3被题意杀了30分钟!!!结果打完之后连system test都测完了…尴尬。最后在practice room里交上去跑了下system test,我是A的……感觉错过了100个亿.jpg结果我T2还fst了..目前rating:1385(蓝名)。

A. FlightDataRecorder

Problem Statement

     A Flight Data Recorder is a device that records lots of parameters during a flight. In this task you are going to use some of those parameters to calculate the distance between the start point and the end point of the flight. The whole flight consists of one or more segments. During each segment the plane was heading in a fixed direction. For each segment, you are given the direction and the distance traveled. More precisely, you are given two vector s: heading and distance. For each valid i: during segment i of the flight the plane traveled in the direction heading[i] and the distance it traveled was distance[i]. The heading is an angle in degrees between North and the direction of flight, measured in clockwise direction. For example, 0 means North, 90 means East, 180 means South, and 270 means West. Assume that the airplane is a point moving in a two-dimensional plane. Also, assume that between the segments the airplane can change its heading in an instant. Calculate and return the distance between the start and the end of the flight.

Definition

Class:
FlightDataRecorder
Method:
getDistance
Parameters:
vector , vector
Returns:
double
Method signature:
double getDistance(vector heading, vector distance)
(be sure your method is public)

Limits

Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256

Notes

Your answer will be considered correct if its absolute or relative error does not exceed 10^(-9).

Constraints

heading will contain between 1 and 50 elements, inclusive.
heading and distance will contain the same number of elements.
Each element in heading will be between 0 and 359, inclusive.
Each element in distance will be between 1 and 1,000, inclusive.

Examples

0)
    {90,0}
    {3,4}
    Returns: 5.0
    The airplane flew 3 units of distance towards the East and then 4 units of distance towards the North. From the Pythagorean theorem we know that the distance between the start and the end of the flight is (33+44)=5.
1)
    {37,37,37,37}
    {1,10,100,1000}
    Returns: 1111.0
    The heading never changed, so the answer is 1+10+100+1000.
2)
    {0,120,240,0,120,240}
    {999,999,999,999,999,999}
    Returns: 6.431098710768743E-13
    The airplane returned back to the start point.
3)
    {76,321,214,132,0,359,74,65,213}
    {621,235,698,1,35,658,154,426,965}
    Returns: 153.54881555325184
4)
    {0}
    {1}
    Returns: 1.0

题意

有一架飞机,给你一堆角度和一堆相对应的长度,表示飞机在第i个时刻朝向第i个角度飞行了第i个长度。0度表示朝北,90度表示朝东,180度表示朝南,270度表示朝西,问你当所有路线飞完后,问你该飞机到原点距离是多少。

思路

这题是最简单的一题,你只要善用cmath库里的sin和cos就行了,只是要注意,cmath里的sin和cos是要用弧度角表示的。所以我们需要先将角度化为弧度再去做就行了。还有,在这里sin对应的是x坐标,cos对应的是y坐标。

Code:

#include<bits/stdc++.h> 
typedef long long ll;
using namespace std;
const double pi=acos(-1);
class FlightDataRecorder {
public:
   double getDistance( vector <int> heading, vector <int> distance ) {
        double posx=0.0,posy=0.0;
        for(int i=0;i<heading.size();i++) {
            posy=posy+cos(heading[i]*pi/180.0)*(double)distance[i];
            posx=posx+sin(heading[i]*pi/180.0)*(double)distance[i];
        }
        return sqrt(posx*posx+posy*posy);
   }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值