Vjudge - E - 这是高中数学向量题

2017-07-15 22:29:06

  •   writer:pprp
  • 评价,用到了叉乘,很麻烦,C++构造知识必须扎实
  • 题目如下:
  • 我们用逆时针方向的顶点序列来表示,我们很想了解这块地的基本情况,现在请你编程判断HDU的用地是凸多边形还是凹多边形呢? 
    Input输入包含多组测试数据,每组数据占2行,首先一行是一个整数n,表示多边形顶点的个数,然后一行是2×n个整数,表示逆时针顺序的n个顶点的坐标(xi,yi),n为0的时候结束输入。 
    Output对于每个测试实例,如果地块的形状为凸多边形,请输出“convex”,否则输出”concave”,每个实例的输出占一行。 
    Sample Input
    4
    0 0 1 0 1 1 0 1
    0
    Sample Output
    convex
  • 
    

     代码如下;

  • #include <iostream>
    
    using namespace std;
    
    class point
    {
    public:
        int x;
        int y;
        point(int a,int b):x(a),y(b) {}
        point():x(0),y(0) {}
        void change(int a,int b)
        {
            x = a;
            y = b;
        }
    };
    
    class vec
    {
    public:
        point a;
        point b;
        point pos;
        point nag;
        vec():a(point(0,0)),
            b(point(0,0)),pos(),nag() {}
        vec(point &aa,point&bb):
            a(aa),b(bb),pos(b.x-a.x,b.y-a.y),nag(a.x-b.x,a.y-b.y) {}
        void change(point & aa,point &bb)
        {
            a.x = aa.x;
            a.y = aa.y;
            b.x = bb.x;
            b.y = bb.y;
    
            pos.x = b.x-a.x;
            pos.y = b.y-a.y;
            nag.x = a.x-b.x;
            nag.y = a.y-b.y;
        }
    };
    
    int chacheng(vec val1,vec val2)
    {
       
        if( (val1.nag.x * val2.pos.y-val1.nag.y*val2.pos.x)> 0)
        {
    
            return 1;
        }
        else
            return 0;
    }
    
    int main()
    {
        int num;
        int a,b;
    
        while(cin >> num && num!=0)
        {
            point *po = new point[num];
            vec *ve = new vec[num];
            for(int i = 0 ; i < num ; i++)
            {
                cin >> a >> b;
                po[i].change(a,b);
            }
            for(int i = 0 ; i < num ; i++)
            {
                if(i != num-1)
                    ve[i].change(po[i],po[i+1]);
                else
                    ve[i].change(po[i],po[0]);
            }
            int cnt = 0;
            for(int i = 0 ; i < num ; i++)
            {
    
                if(i == num-1)
                {
                    if(chacheng(ve[i],ve[0])==1)
                    {
                        cnt++;
                    }
                }
                else
                {
                    if(chacheng(ve[i],ve[i+1])==1)
                    {
                        cnt++;
                    }
                }
            }
            if(cnt == 0 || cnt == num)
            {
                cout << "convex" << endl;
            }
            else
            {
                cout << "concave"<<endl;
            }
        }
        return 0;
    }

     

posted @ 2017-07-15 22:30  pprp  阅读(223)  评论(0编辑  收藏  举报