每日一题2023-1-2

题目描述

题意
给出n个人在平面XOY上的位置,其中的k个人具有光源,现问所有人离光源最近的距离的最大值是多
少,与标准答案的误差不超过即10-5可 。

输入格式:
第一行输入两个正整数n,k
第二行输入k个整数,表示第ai个人具有光源
接下来n行每行包括两个整数,表示第i个人的位置坐标

输出格式:
输出一个实数表示结果

样例输入1:
4 2
2 3
0 0
0 1
1 2
2 0

样例输出1
2.23606797749978969

样例输入2
2 1
2
-100000 -100000
100000 100000

样例输出2
282842.712474619009

样例输入3
8 3
2 6 8
-17683 17993
93038 47074
58079 -57520
-41515 -89802
-72739 68805
24324 -73073
71049 72103
47863 19268

样例输出3
130379.280458974768

解题思路

将具有光源的人的序号、具有光源的点的位置与非光源点的位置分别存入到三个集合a、isLight、notLight中,判断第(i+1)个序号是否在光源序号集合a中(下标从0开始,但是数光源是从1开始的),然后进行分类添加到光源集合和非光源集合中,计算各点到各个光源点的距离进行判断取最小值,最后再在最小值中取出最大值,即可得到结果。

JAVA源码

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n,k;
        n = sc.nextInt();// n个人
        k = sc.nextInt();// k个人具有光源

        ArrayList<Integer> a = new ArrayList<>();// 光源序号集合
        ArrayList<int[]> notLight = new ArrayList<>();// 非光源集合
        ArrayList<int[]> isLight = new ArrayList<>();// 光源集合

        //第二行k个整数,表示第ai个人具有光源
        for (int i = 0; i < k; i++) {
            a.add(sc.nextInt());
        }

        //接下来n行输入第i个人的坐标位置xi,yi
        for (int i = 0; i < n; i++) {
            if (a.contains(i+1)){// 加入光源集合
                isLight.add(new int[]{sc.nextInt(),sc.nextInt()});
            }
            else{ // 加入非光源集合
                notLight.add(new int[]{sc.nextInt(),sc.nextInt()});
            }
        }

        double max = 0;
        for (int[] ints:notLight){
            double min = Double.MAX_VALUE;
            for (int[] ints1:isLight){
                double pow = Math.pow(Math.pow(Math.abs(ints[0] - ints1[0]),2) + Math.pow(Math.abs(ints[1] - ints1[1]),2),0.5);
                if (pow < min){
                    min = pow;
                }
            }
            if(min > max){
                max = min;
            }
        }
        System.out.println(max);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值