ccf-csp认证 202009-1称检测点查询

本文比较了C++和Java编程语言实现了一个计算两点间距离并按距离排序的程序,C++使用了标准库函数,而Java则使用了PriorityQueue。
摘要由CSDN通过智能技术生成

C++版:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 210;
struct Point{
    int seq;
    int d;
    bool operator < (const Point &p) const{
        if(d == p.d) return seq < p.seq;
        else return d < p.d;
    }
}point[N];
int dist(int x1, int y1, int x, int y){
    return pow((x1 - x), 2) + pow((y1 - y), 2);
}
int main(){
    int n, x, y;
    int x1, y1;
    scanf("%d%d%d", &n, &x, &y);
    for(int i = 1; i <= n; i++){
        scanf("%d%d", &x1, &y1);
        int dd = dist(x1, y1, x, y);
        point[i] = {i, dd};
    }
    sort(point + 1, point + n + 1);
    for(int i = 1; i <= 3; i++){
        printf("%d\n", point[i].seq);
    }
}

java版:


import java.lang.*;
import java.util.*;
import java.io.*;
public class Main {
    static int N = 210;
    public static double dist(int x1,int y1, int x, int y){
        return Math.pow((x1 - x), 2) + Math.pow((y1 - y), 2);
    }
    public static void  main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] param = br.readLine().split(" ");
        int n = Integer.parseInt(param[0]);
        int x = Integer.parseInt(param[1]);
        int y = Integer.parseInt(param[2]);
        PriorityQueue<int[]> q = new PriorityQueue<>(new Comparator<int[]>() {
            public int compare(int[] o1, int[] o2){
                if(o1[1] != o2[1]) return o1[1] - o2[1];
                else return o1[0] - o2[0];
            }
        });
        for(int i = 1; i <= n; i++){
            param = br.readLine().split(" ");
            int x1 = Integer.parseInt(param[0]);
            int y1 = Integer.parseInt(param[1]);
            q.add(new int[]{i, (int)dist(x1, y1, x, y)});
        }
        for(int i = 1; i < 4; i++){
            System.out.println(q.poll()[0]);
        }
        br.close();
    }
}

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值