1184: 平面点排序(二)(结构体专题)

xiaoxiao2021-02-28  103

Description

平面上有n个点,坐标均为整数。横坐标相同时按纵坐标排序,否则按横坐标排序。本题要求用结构体存储坐标,再进行排序。先升序排序输出,再降序排序输出,可以自己写排序函数,也可以用qsort库函数排序。

Input

第一行是整数n(1<=n<=100),表示接下来有n行,每行两个整数,表示平面上一个点的坐标。

Output

输出有两行,即排序后的点,格式为(u,v),每个点后有一个空格。第一行升序排序结果,第二行降序排序结果。

Sample Input

41 32 51 44 1

Sample Output

(1,3) (1,4) (2,5) (4,1) (4,1) (2,5) (1,4) (1,3)

HINT

Source

#include <stdio.h> #include <stdlib.h> typedef struct Point { int x; int y; int point; } P; int main() { P p[102],temp; int n; int i,j; scanf("%d",&n); for(i=0; i<n; i++) { scanf("%d%d",&p[i].x,&p[i].y); p[i].point=p[i].x* p[i].x+ p[i].y*+ p[i].y; } for(i=0; i<n; i++) { for(j=i+1; j<n; j++) { if( p[i].x > p[j].x) { temp=p[i]; p[i]=p[j]; p[j]=temp; } else if(p[i].x==p[j].x) { if( p[i].y>p[j].y) { temp=p[i]; p[i]=p[j]; p[j]=temp; } } } } for(i=0; i<n; i++) { printf("(%d,%d) ",p[i].x, p[i].y); } printf("\n"); for(i=n-1; i>=0; i--) { printf("(%d,%d) ",p[i].x, p[i].y); } printf("\n"); return 0; }
转载请注明原文地址: https://www.6miu.com/read-18816.html

最新回复(0)