hdu 1785(对向量排序)

 1 /*
 2 *  题目要求:对一组向量按与x轴的正向夹角从小到大排序
 3 *  解法:借用求凸包的排序规则即可 
 4 */
 5 
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <cstdlib>
 9 #include <iostream>
10 
11 using namespace std;
12 
13 const int N = 105;
14 const double eps = 1e-8;
15 
16 struct point {
17     double x;
18     double y;
19 }p[N];
20 
21 bool isZero(double x) {
22     return (x > 0 ? x : -x) < eps;
23 }
24 
25 double dis(point A, point B) {
26     return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
27 }
28 
29 double crossProd(point A, point B, point C) {
30     return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x);
31 }
32 
33 int cmp(const void *a, const void *b) {//按x轴的正向夹角从小到大排序
34     point *c = (point *)a;
35     point *d = (point *)b;
36     double k = crossProd(p[0], *c, *d);
37     if (k<eps || (isZero(k)&&dis(p[0], *c)>dis(p[0], *d))) return 1;
38     return -1;
39 }
40 
41 void solve(int n) {
42     qsort(p+1, n, sizeof(point), cmp);
43     return ;
44 }
45 
46 int main() {
47     int n;
48     p[0].x = p[0].y = 0;
49     while (scanf("%d", &n) && n >= 1) {
50         for (int i=1; i<=n; ++i) scanf ("%lf%lf", &p[i].x, &p[i].y);
51         solve(n);
52         printf ("%.1lf %.1lf", p[1].x, p[1].y);
53         for (int i=2; i<=n; ++i) printf (" %.1lf %.1lf", p[i].x, p[i].y);
54         printf ("\n");
55     }
56     return 0;
57 }

 

转载于:https://www.cnblogs.com/try86/archive/2012/04/24/2468960.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值