(hdu step 7.1.1)Shape of HDU(推断一个多边形是否是凸多边形)

题目:

Shape of HDU

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 159 Accepted Submission(s): 97
 

Problem Description

话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后。“徐队”的称呼逐渐被“徐总”所代替,海东集团(HDU)也算是名副事实上了。

创业是须要地盘的,HDU向钱江肉丝高新技术开发区申请一块用地。非常快得到了批复,据说这是由于他们公司研发的“海东牌”老鼠药科技含量非常高。预期将占全球一半以上的市场。政府划拨的这块用地是一个多边形。为了描写叙述它,我们用逆时针方向的顶点序列来表示,我们非常想了解这块地的基本情况,如今请你编程推断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

海东集团最终顺利成立了!后面的路,他们会顺顺利利吗?
欲知后事怎样,且听下回分解——


Author

lcy


Source

ACM程序设计_期末考试(时间已定!!)


Recommend

lcy

题目分析:

简单题,模板题,推断一个多边形是否是凸多边形。

这里并不须要计算出凸包,仅仅须要简单推断一下就好。

这里用到了叉乘的性质。

叉乘的性质:设两向量P和Q

1.P ×Q > 0 则Q在P的逆时针方向

2.P ×Q < 0 则Q在P的顺时针方向

3.P ×Q = 0 则Q和P共线,方向可能同样也可能不同样

代码例如以下:

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const int maxn = 100000;
struct Point {
	double x, y;
	Point() {
	}
	Point(double _x, double _y) {
		x = _x;
		y = _y;
	}

	Point operator -(const Point &B) const {
		return Point(x - B.x, y - B.y);
	}

} p[maxn];

double eps = 1e-10;
int dcmp(double x) {
	if (fabs(x) < eps)
		return 0;
	else
		return x < 0 ? -1 : 1;
}
double Cross(Point A, Point B) {
	return A.x * B.y - A.y * B.x;
}
/**
 * 推断多边形是否是凸多边形【含共线】
 */
bool isConvex(Point *p, int n) {
	p[n] = p[0]; // 边界处理
	p[n + 1] = p[1]; // 注意也能够用 %n 处理, 下标从 0 開始
	int now = dcmp(Cross(p[1] - p[0], p[2] - p[1]));
	for (int i = 1; i < n; i++) {
		int next = dcmp(Cross(p[i + 1] - p[i], p[i + 2] - p[i + 1]));
		if (now * next < 0) {//此处能够共线
			return false;
		}
		now = next; //注意记录临界条件
	}

	return true;
}

int main() {
	int n;
	while (scanf("%d", &n) != EOF, n) {
		int i;
		for (i = 0; i < n; ++i) {
			scanf("%lf %lf", &p[i].x, &p[i].y);
		}

		bool flag = isConvex(p, n);
		if (flag == true) {
			printf("convex\n");
		} else {
			printf("concave\n");
		}
	}

	return 0;
}
时间: 2024-08-29 04:45:12

(hdu step 7.1.1)Shape of HDU(推断一个多边形是否是凸多边形)的相关文章

(hdu 7.1.1)Shape of HDU(判断一个多边形是否是凸多边形)

题目: Shape of HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 159 Accepted Submission(s): 97   Problem Description 话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后,"徐队"的称呼逐渐被"徐总"所取代,海东集团(

(hdu step 1.3.5)Fighting for HDU(排序)

题目: Fighting for HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2412 Accepted Submission(s): 1210   Problem Description 在上一回,我们让你猜测海东集团用地的形状,你猜对了吗?不管结果如何,都没关系,下面我继续向大家讲解海东集团的发展情况:在最初的两年里,HDU发

(hdu step 1.2.8)Specialized Four-Digit Numbers(求一个数字各个数位上数字的和)

题目: Specialized Four-Digit Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2027 Accepted Submission(s): 1349   Problem Description Find and list all four-digit numbers in decimal notation

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

(hdu step 1.3.1)FatMouse&#39; Trade(在收入需要一定的付出的情况下求最大收入)

题目: FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5092 Accepted Submission(s): 1530   Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gua

hdu 2108 Shape of HDU (数学)

Shape of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5059    Accepted Submission(s): 2294 Problem Description 话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后,"徐队"的称呼逐渐被"徐总"所取代,海东集

(hdu step 6.1.2)Eddy&#39;s picture(在只给出二维坐标点的情况下,求让n个点连通的最小费用)

题目: Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 172 Accepted Submission(s): 126   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be

(hdu step 5.1.1)A Bug&#39;s Life((ai,bi)表示ai、bi不在同一堆中,有若干对数据,判断是否有bug)

题目: A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 723 Accepted Submission(s): 277   Problem Description Background Professor Hopper is researching the sexual behavior of a rare spe

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble