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 }

 

你可能感兴趣的:(HDU)