1183: Planar point sequencing (i) (structural features) time limit: 1 Sec Memory Limit: MB
Submit: 803 Solved: 555
Submitstatusweb Board Description
There are n points on the plane, and the coordinates are integers. Sort all points by the distance from the origin of the coordinates (0,0). You can write your own sort functions, or you can sort them with qsort library functions.
Input
The input has two lines, the first line is an integer n (1<=n<=10), followed by n rows, and a pair of integers per line (one point for each pair of integers).
Output
Outputs all the points after sorting, in the format (U,V), with a space after each point. The test data guarantees that the distance from each point to the origin is different.
Sample Input3 2 5 1 4 4 2Sample Output( 1,3) (1,4) (4,2) (2,5)HINT
SOURCE Exercise:
#include <iostream> #include <stdio.h> #include <algorithm>using namespace std;struct point{ int x; int y; int JLP;}; int CMP (point A,point b) { return a.jlp<b.jlp;//ascending}int main () { int n,i; Point a[10]; while (scanf ("%d", &n)!=eof) {for (i=0;i<n;i++) { cin>>a[i].x>>a[i].y; a[i].jlp=a[i].x*a[i].x+a[i].y*a[i].y; } Sort (a,a+n,cmp); for (i=0;i<n;i++) { cout<< "(" <<a[i].x<< "," <<a[i].y<< ")" << ""; } } return 0;}
Sort of planar points (I.) (Structural topics)