#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
int x,y;
};
node vex[];//存入的所有的点
node stackk[];//凸包中所有的点
int xx,yy;
bool cmp1(node a,node b)//排序找第一个点
{
if(a.y==b.y)
return a.x<b.x;
else
return a.y<b.y;
}
bool cmp(node a,node b)//排序找第一个点
{
if(a.x==b.x)
return a.y<b.y;
else
return a.x<b.x;
}
int cross(node a,node b,node c)//计算叉积
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
bool cmp2(node a,node b)//极角排序另一种方法,速度快
{
if(atan2(a.y-yy,a.x-xx)!=atan2(b.y-yy,b.x-xx))
return (atan2(a.y-yy,a.x-xx))<(atan2(b.y-yy,b.x-xx));
return a.x<b.x;
}
int main()
{
int t,L,n,m;
cin>>n;
while(n--)
{
cin>>m;
int i;
for(i=; i<m; i++)
{
scanf("%d%d",&vex[i].x,&vex[i].y);
} memset(stackk,,sizeof(stackk)); sort(vex,vex+m,cmp1);
stackk[]=vex[];
xx=stackk[].x;
yy=stackk[].y;
sort(vex+,vex+m,cmp2);//cmp2是更快的,cmp更容易理解
stackk[]=vex[];//将凸包中的第两个点存入凸包的结构体中
int top=;//最后凸包中拥有点的个数
for(i=; i<m; i++)
{
while(i>=&&cross(stackk[top-],stackk[top],vex[i])<) //对使用极角排序的i>=1有时可以不用,但加上总是好的
top--;
stackk[++top]=vex[i]; //控制<0或<=0可以控制重点,共线的,具体视题目而定。
} sort(stackk,stackk+top+,cmp);
for(i=;i<=top;i++)
printf("%d %d\n",stackk[i].x,stackk[i].y); }
}
相关文章
- nyoj 开方数
- NYOJ 42 一笔画
- NYOJ 42 一笔画问题
- nyoj 42 一笔画 欧拉通路
- (实战)[re:Invent 2018]-002:通过分析奖励函数优化后- 8.4s / 圈
- Nyoj 修路方案(次小生成树)
- 算法训练 - 筛选号码 (有n个人围成一圈,顺序排号(编号为1到n)。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子。从下一个人开始继续报数,直到剩下最后一个人,游戏结束。)
- n个人(编号 1~n)围成一- 圈从编号为1的开始报数,从1报数到m;报到m的人出来
- #include <> typedef struct Node { int index; struct Node *next; }JosephuNode; int Josephu(int n, int m) { int i, j; JosephuNode *head, *tail; head = tail = (JosephuNode *)malloc(sizeof(JosephuNode)); for (i = 1; i < n; ++i) { tail->index = i; tail->next = (JosephuNode *)malloc(sizeof(JosephuNode)); tail = tail->next; } tail->index = i; tail->next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号\n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; }">设编号为1,2,… n的n个人围坐一圈,约定编号为k(1数组实现: #include <> #include <> int Josephu(int n, int m) { int flag, i, j = 0; int *arr = (int *)malloc(n * sizeof(int)); for (i = 0; i < n; ++i) arr[i] = 1; for (i = 1; i < n; ++i) { flag = 0; while (flag < m) { if (j == n) j = 0; if (arr[j]) ++flag; ++j; } arr[j - 1] = 0; printf("第%4d个出局的人是:%4d号\n", i, j); } free(arr); return j; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; } 链表实现: #include <> #include <> typedef struct Node { int index; struct Node *next; }JosephuNode; int Josephu(int n, int m) { int i, j; JosephuNode *head, *tail; head = tail = (JosephuNode *)malloc(sizeof(JosephuNode)); for (i = 1; i < n; ++i) { tail->index = i; tail->next = (JosephuNode *)malloc(sizeof(JosephuNode)); tail = tail->next; } tail->index = i; tail->next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号\n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; }
- 如何用C语言实现圈叉游戏(-)