Project Eular 144

这题...关键在于.....计算几何能力...

atan2的用法应该是

atan2(y,x)而不是(x,y)

代码:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<math.h>
#include<time.h>
#include<vector>
#include<bitset>
#include<memory>
#include<utility>
#include<fstream>
#include<stdio.h>
#include<sstream>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
const double eps=1e-8;
struct point
{
    double x;
    double y;
    point (double xx=0,double yy=0)
    {
        x=xx;
        y=yy;
    }
    bool is_ans()
    {
        if (((-0.01<x)&&(x<0.01))&&(y>0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    double el_k()
    {
        return -4*x/y;
    }
    friend point operator + (const point &a,const point &b)
    {
        return point(a.x+b.x,a.y+b.y);
    }
    friend point operator * (const point &a,double b)
    {
        return point (a.x*b,a.y*b);
    }
    friend point operator / (const point &a,double b)
    {
        return point (a.x/b,a.y/b);
    }
    double val()
    {
        return 4*x*x+y*y;
    }
    void go(point k)
    {
        point l=point(x,y)+k*0.1;
        point r=point(x,y)+k*100;
        point mid;
        int i;
        for (i=0;i<=1000;i++)
        {
            mid=(l+r)/2;
            if (mid.val()<100)
            {
                l=mid;
            }
            else
            {
                r=mid;
            }
        }
        x=mid.x;
        y=mid.y;
    }
};
int main()
{
    point k=point(1.4,-(10.1+9.6));
    point now;
    now.x=1.4;
    now.y=-9.6;
    int ans=0;
    for (;;)
    {
        if (now.is_ans()) break;
        ans++;
        double kk=now.el_k();
        double t1=atan2(k.y,k.x);
        double t2=atan(kk);
        t1=t2*2-t1;
        k.x=cos(t1);
        k.y=sin(t1);
        now.go(k);
    }
    printf("%d\n",ans);
    system("pause");
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/absi2011/p/9236196.html