include stdio include stdlib typedef struct Point int POINT int compar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
typedef struct Point {
int x, y;
} POINT;
int compare(const void *x, const void *y) {
POINT a = *(const POINT*)x;
POINT b = *(const POINT*)y;
int r1 = a.x*a.x + a.y*a.y;
int r2 = b.x*b.x + b.y*b.y;
if (r1 == r2) {
return 0;
}
return (r1 < r2) ? -1 : 1;
}
int main() {
int N, i;
POINT *arr;
scanf("%d", &N);
arr = (POINT*)malloc(N*sizeof(POINT));
for (i = 0; i < N; i++) {
scanf("%d %d", &arr[i].x, &arr[i].y);
}
qsort(arr, N, sizeof(POINT), compare);
for (i = 0; i < N; i++) {
printf("%d %d\n", arr[i].x, arr[i].y);
}
free(arr);
return 0;
}