标签:sys include 注意 ret blog use false als 查看
给定一个点的坐标(x, y),在输入的n个点中,依次计算这些点到指定点的距离,并按照距离进行从小到大排序,并且输出点的坐标(如果距离相同,将x轴坐标比较小的点排到前面, 如果距离相等且x轴坐标也相同,则将y轴坐标较小的点排到前面)。坐标为int类型,范围为-1000到1000。n 为1到100之间正整数。
0 0 5 1 0 0 1 1 1 5 2 4 0
(0,1), (1,0), (1,1), (4,0), (5,2)
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm> using namespace std; struct E{ int x, y; int dis; }buf[101]; bool cmp(E a, E b) { if(a.dis != b.dis) return a.dis < b.dis; else if(a.x != b.x) return a.x < b.x; else if(a.y != b.y) return a.y < b.y; return false; } int main() { int a, b; scanf("%d%d", &a, &b); int n; scanf("%d", &n); for(int i = 0; i < n; i++ ){ scanf("%d%d", &buf[i].x, &buf[i].y); buf[i].dis = (buf[i].x - a)*(buf[i].x - a) + (buf[i].y - b)*(buf[i].y - b); } sort(buf, buf+n, cmp); for(int i = 0; i < n-1; i++) { printf("(%d,%d), ", buf[i].x, buf[i].y); } printf("(%d,%d)", buf[n-1].x, buf[n-1].y); system("PAUSE"); return 0; }
because the exam is end, I don‘t know whether my answer is right
bailian-chayuan-2017-A-point sort
标签:sys include 注意 ret blog use false als 查看
原文地址:http://www.cnblogs.com/QingHuan/p/7151477.html