首页 > 代码库 > TopCoder SRM 633 Div.2 500 Jumping

TopCoder SRM 633 Div.2 500 Jumping

题意:给一个点(x,y),给一些步长delta1,delta2...deltaN,问从(0,0)严格按照步长走完N步后能否正好到达(x,y)点。

解法:其实就是判断这些线段和(0,0)-(x,y)这条线段能否构成一个多边(角?)形的问题,只需判断最长的边是否不大于于所有边长和的一半即可。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>using namespace std;class Jumping{public:    string ableToGet(int x, int y, vector <int> jumpLengths)    {        double dis = sqrt(x*x+y*y);        double sum = dis,maxi = dis;        for(int i=0;i<jumpLengths.size();i++)        {            sum += (double)jumpLengths[i];            maxi = max(maxi,(double)jumpLengths[i]);        }        if(sum-maxi >= maxi)            return "Able";        return "Not able";    }};
View Code

 

TopCoder SRM 633 Div.2 500 Jumping