0
点赞
收藏
分享

微信扫一扫

1183: 平面点排序(一)(结构体专题)

i奇异 2022-02-08 阅读 68

题目描述
平面上有n个点,坐标均为整数。请按与坐标原点(0,0)距离的远近将所有点排序输出。可以自己写排序函数,也可以用qsort库函数排序。

输入
输入有两行,第一行是整数n(1<=n<=10),接下来有n行,每行一对整数(每对整数对应一个点)。

输出
输出排序后的所有点,格式为(u,v),每个点后有一个空格。测试数据保证每个点到原点的距离都不同。

样例输入 Copy
4
1 3
2 5
1 4
4 2
样例输出 Copy
(1,3) (1,4) (4,2) (2,5)

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
//定义结构体 
typedef struct{
	int x;
	int y;
}point;
//比较函数:根据距离公式比较两个点距离原点大小 
int cmp(point a, point b){
	return sqrt(a.x * a.x + a.y * a.y) < sqrt(b.x * b.x + b.y * b.y);
}

int main(){
	point p[15];
	int n;
	cin >> n;
	//循环输入点 
	for(int i = 0; i < n; i++){
		scanf("%d %d", &p[i].x, &p[i].y);
	}
	//排序 
	sort(p, p + n, cmp);
	
	//输出 
	for(int i = 0; i < n; i++){
		printf("(%d,%d) ", p[i].x, p[i].y);
	}

	return 0;
} 
举报

相关推荐

0 条评论