zoukankan      html  css  js  c++  java
  • 凸包求周长

    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <cstdio>
    using namespace std;
    int n;
    typedef struct
    {
        double x;
        double y;
    }Point;
    
    Point p[110],s[110];
    
    int top;
    
    double Mul(Point a,Point b,Point c)
    {
        return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
    }
    
    double dis(Point a,Point b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    
    int cmp(const void *a,const void *b)
    {
        Point c=*(Point *)a;
        Point d=*(Point *)b;
        double k=Mul(p[0],c,d);
        if(k<0||(!k&&dis(c,p[0])>dis(d,p[0]))) return 1;
        return -1;
    }
    
    void con()
    {
        int i;
        for(i=1;i<n;i++)
        {
            Point temp;
            if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x))
            {
                temp=p[i];
                p[i]=p[0];
                p[0]=temp;
            }
        }
        qsort(p+1,n-1,sizeof(p[0]),cmp);
        s[0]=p[0];
        s[1]=p[1];
        s[2]=p[2];
        top=2;
        for(i=3;i<n;i++)
        {
            while(top>=2&&Mul(s[top-1],s[top],p[i])<=0) top--;
            top++;
            s[top]=p[i];
        }
    }
    
    int main()
    {
        while(cin>>n,n)
        {
            int i;
            for(i=0;i<n;i++)
            {
                cin>>p[i].x>>p[i].y;
            }
            if(n==1)
            {
                cout<<"0.00"<<endl;
                continue;
            }
            else if(n==2)
            {
                printf("%.2f
    ",dis(p[0],p[1]));
                continue;
            }
            con();
            double coun=0;
            for(i=0;i<top;i++)
            {
                coun+=dis(s[i],s[i+1]);
            }
            coun+=dis(s[top],s[0]);
            printf("%.2f
    ",coun);
        }
        return 0;
    }
  • 相关阅读:
    2017级面向对象程序设计 作业三
    2017级面向对象程序设计 作业二
    2017级面向对象程序设计 作业一
    寒假作业之总结
    寒假第三次作业
    寒假第二次作业 与电梯有关的代码问题
    我印象中最深刻的三位老师
    Alpha冲刺Day5
    Alpha冲刺Day4
    Alpha冲刺Day3
  • 原文地址:https://www.cnblogs.com/zuferj115/p/5001636.html
Copyright © 2011-2022 走看看