zoukankan      html  css  js  c++  java
  • hud 4454 Stealing a Cake 解题报告

    题意:给定一个点 一个圆,一个矩形,求点到圆上再到矩形的最短距离

    解题思路:三分圆上点的弧度

    解题代码:

    #include <stdio.h>
    #include <math.h>
    double ans ;
    double x1,y11,x2,y2,x,y,r,cx,cy;
    
    void fen(double low ,double high)
    {
        double dis(double a,double b,double c,double d);
        double dis1(double i , double j );
    
        double tx , ty;
        double ansmid1 ;
        double ansmid2 ;
        double mid1 = low+(high-low)*1/3;
        tx = x + r*sin(mid1);
        ty = y + r*cos(mid1);
        ansmid1 = dis(tx,ty,cx,cy) + dis1(tx,ty);
        //printf("%lf %lf %lf\n",tx,ty,ansmid1);
        double mid2 = low +(high - low)*2/3;
        tx = x + r*sin(mid2);
        ty = y + r*cos(mid2);
        ansmid2 = dis(tx,ty,cx,cy) + dis1(tx,ty);
        //printf("%lf %lf %lf\n",tx,ty,ansmid2);
        if(fabs(ansmid2 - ansmid1) < 0.00001)
        {
           ans = ansmid2;
           return ;
        }
        if(ansmid1 < ansmid2)
            fen(low,mid2);
        else
            fen(mid1,high);
    
    }
    void fen2(double low ,double high)
    {
        double dis(double a,double b,double c,double d);
        double dis1(double i , double j );
    
        double tx , ty;
        double ansmid1 ;
        double ansmid2 ;
        double mid1 = low+(high-low)*1/4;
        tx = x + r*sin(mid1);
        ty = y + r*cos(mid1);
        ansmid1 = dis(tx,ty,cx,cy) + dis1(tx,ty);
        //printf("%lf %lf %lf %lf %lf\n",tx,ty,ansmid1,dis(tx,ty,cx,cy),dis1(tx,ty));
        double mid2 = low +(high - low)*2/4;
        tx = x + r*sin(mid2);
        ty = y + r*cos(mid2);
        ansmid2 = dis(tx,ty,cx,cy) + dis1(tx,ty);
        //printf("%lf %lf %lf\n",tx,ty,ansmid2);
        if(fabs(ansmid2 - ansmid1) < 0.00001)
        {
           
           if(ansmid2 < ans)
                 ans = ansmid2;
           return ;
        }
        if(ansmid1 < ansmid2)
            fen2(low,mid2);
        else
            fen2(mid1,high);
    
    }
    double dis(double a,double b,double c,double d)
    {
        return sqrt(pow(a-c,2)+pow(b-d,2));
    }
    double dis1(double i , double j )
    {
     
        if(i >= x1 && i <= x2)
        {
            if(j > y11)
            {
                return fabs(j-y11);
            }
            else if(j < y2)
            {
                return  fabs(y2 - j);
            }
            else return 0 ;
        }
        else if(j >= y2 && j <= y11)
        {
            if(i < x1 )
            {  
                return fabs(x1 - i);
            }
            else if(i >x2 )
            {
                return fabs(i-x2);
            }
            else return 0 ;
    
        }
        else if(i < x1 && j > y11)
        {
            return dis(i,j ,x1, y11);
        }
        else if(i > x2 && j > y11)
        {
            return dis(i,j,x2,y11);
        }
        else if(i < x1 && j < y2)
        {
            return dis(i , j , x1, y2);
        }
        else return dis(i,j,x2,y2);
    }
    int main()
    {
        while(scanf("%lf %lf",&cx,&cy) != EOF)
        {
            if(cx == 0 && cy == 0)
                break;
            scanf("%lf %lf %lf",&x,&y,&r);
            scanf("%lf %lf %lf %lf",&x1,&y11,&x2,&y2);
            double tempx, tempy;
            if(x1 < x2 && y11 > y2)
            {
                int p ;
            }
            else if(x1 < x2 && y11 < y2)
            {
                tempy = y11;
                y11 = y2;
                y2 = tempy;
            }
            else if(x1 > x2 && y11 > y2)
            {
                tempx = x1 ;
                x1 = x2;
                x2 = tempx;
            }
            else if(x1 > x2 && y11 < y2)
            {
                tempy = y11;
                y11 = y2;
                y2 = tempy;
                tempx = x1 ;
                x1 = x2;
                x2 = tempx;
    
            }
            //printf("%lf %lf %lf %lf\n",x1,y11,x2,y2);
            fen(0,3.1415926*2);
            fen2(0,3.1415926*2);
            printf("%.2lf\n",ans);
        }
        return 0 ;
    } 
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    怎么防止重复提交
    JSP三大指令是什么?
    python中字符串的编码和解码
    什么是 JavaConfig?
    Spring Boot 有哪些优点?
    如何重新加载 Spring Boot 上的更改,而无需重新启动服务器?
    Mybatis 动态 sql 是做什么的?都有哪些动态 sql?能简述一下动态 sql 的执行原理不?
    如果你也用过 struts2.简单介绍下 springMVC 和 struts2 的区别有哪些?
    学习笔记——命令模式Command
    学习笔记——中介者模式Mediator
  • 原文地址:https://www.cnblogs.com/zyue/p/3108792.html
Copyright © 2011-2022 走看看