andre_joy

导航

hdu 2108

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2108

题意:给定一个多边形的每一个顶点坐标,判断它是否为凸多边形。

mark:判断每两个向量的转向。wa了2次,tle了1次。把文件输入输出部分一起交了……

代码:

#include <stdio.h>

int main()
{
    int n,x1,x2,x3,y1,y2,y3,f,x0,y0,x4,y4;
    while(scanf("%d", &n), n)
    {
        if(n < 3)
        {
            while(n--)
                scanf("%d%d", &x0, &y0);
            puts("convex");
            continue;
        }
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        x0 = x1;
        y0 = y1;
        x4 = x2;
        y4 = y2;
        n--;
        f = 0;
        while(--n && scanf("%d%d", &x3, &y3))
        {
            if(f) continue;
            if((x2-x1)*(y3-y1) - (x3-x1)*(y2-y1) < 0) f = 1;
            x1 = x2;
            y1 = y2;
            x2 = x3;
            y2 = y3;
        }
        if((x2-x1)*(y0-y1) - (x0-x1)*(y2-y1) < 0) f = 1;
        if((x0-x2)*(y4-y2) - (x4-x2)*(y0-y2) < 0) f = 1;
        puts(f ? "concave" : "convex");
    }
    return 0;
}

posted on 2012-07-08 18:26  andre_joy  阅读(141)  评论(0)    收藏  举报