3183. 坐标排序

单点时限: 2.0 sec

内存限制: 256 MB

对 $n$ $(1<n \leq 100)$ 个点组成的数组按以下顺序排序:

按在坐标系统中点与原点之间的距离从小到大排序。距离相同时按第 1 象限到第 4 象限的顺序排序。如果距离相同又在同一象限内,则按坐标值 x 的绝对值从小到大排序。

点的类型定义为:

typedef struct {
    int x,y;
} Point;

只需按要求写出函数定义,并使用给定的测试程序测试你所定义函数的正确性。
不要改动测试程序。测试正确后,将测试程序和函数定义一起提交。

//********** Specification of SortPoints **********
void SortPoints(Point *p, int n);
/* PreCondition:
          p points to an array with n coordinate points
    PostCondition:
          array is sorted satisfying to the specification
*/

/***************************************************************/
/*                                                             */
/*  DON'T MODIFY main function ANYWAY!                         */
/*                                                             */
/***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#define N 100
typedef struct {
    int x,y;
} Point;
/********** Specification of SortPoints **********/
void SortPoints(Point *p, int n)
/* PreCondition:
p points to an array with n coordinate points
PostCondition:
array is sorted satisfying to the specification
*/
{   //TODO: your function definition
}
/***************************************************************/
int main()
{
    Point a[N];
    int n,i,t,cas;
    scanf("%d",&cas);
    for(t=0; t<cas; t++)
    {
        scanf("%d",&n);
        for (i=0; i<n; i++) scanf("%d%d",&a[i].x,&a[i].y);
        /***** function SortPoints is called here *****/
        SortPoints(a,n);
        /****************************************/
        printf("case #%d:\n",t);
        for (i=0; i<n; i++) printf("(%d,%d)%c",a[i].x,a[i].y,i<n-1?' ':'\n');
    }
    return 0;
}

117 人解决,125 人已尝试。

136 份提交通过,共有 326 份提交。

2.5 EMB 奖励。

创建: 7 年,4 月前.

修改: 6 年,7 月前.

最后提交: 4 月,2 周前.

来源: N/A