Newcoder 110 B.简单多边形(计算几何)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/V5ZSQ/article/details/83109924

Description

为了让所有选手都感到开心, N o w c o d e r Nowcoder 练习赛总会包含一些非常基本的问题。 比如说:

按顺时针或逆时针方向给你一个简单的多边形的顶点坐标,请回答此多边形是顺时针还是逆时针。

Input

输入包含 N + 1 N + 1 行。
第一行包含一个整数 N N ,表示简单多边形的顶点数。
在下面的 N N 行中,第 i i 行包含两个整数 x i , y i x_i,y_i ,表示简单多边形中的第 i i 个顶点的坐标。

( 3 n 30 , 1000 x i , y i 1000 ) (3\le n\le 30,-1000\le x_i,y_i\le 1000)

Output

如果简单多边形按顺时针顺序给出,则在一行中输出 c l o c k w i s e “clockwise” (不带引号)。 否则,打印 c o u n t e r c l o c k w i s e “counterclockwise” (不带引号)。

Sample Input

3
0 0
1 0
0 1

Sample Output

扫描二维码关注公众号,回复: 3640051 查看本文章

counterclockwise

Solution

求出该多边形有向面积判断正负即可

Code

#include<cstdio>
using namespace std;
int n,x[33],y[33];
int S(int i,int j)
{
	return x[i]*y[j]-x[j]*y[i];
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%d%d",&x[i],&y[i]); 
	x[n]=x[0],y[n]=y[0];
	int res=0;
	for(int i=0;i<n;i++)res+=S(i,i+1);
	if(res>0)printf("counterclockwise\n");
	else printf("clockwise\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/V5ZSQ/article/details/83109924
110