PTA乙级1082

1082

本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军;谁差得最远,谁就是菜鸟。本题给出一系列弹洞的平面坐标(x,y),请你编写程序找出冠军和菜鸟。我们假设靶心在原点(0,0)。

输入格式:

输入在第一行中给出一个正整数 N(≤ 10 000)。随后 N 行,每行按下列格式给出:

ID x y
其中 ID 是运动员的编号(由 4 位数字组成);x 和 y 是其打出的弹洞的平面坐标(x,y),均为整数,且 0 ≤ |x|, |y| ≤ 100。题目保证每个运动员的编号不重复,且每人只打 1 枪。

输出格式:

输出冠军和菜鸟的编号,中间空 1 格。题目保证他们是唯一的。

输入样例:

3
0001 5 7
1020 -1 3
0233 0 -1

输出样例:

0233 0001

思路:这题我用了hashmap存储参赛者编号与距离,然后通过获取最大与最小距离再通过距离这个值获取key也就是编号

代码:

package Test_PTA_1062to;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class PTA1082 {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int N=Integer.parseInt(br.readLine());
        String[] b=new String[N];
        for(int i=0;i<N;i++){
            b[i]=br.readLine();
        }
        Map<String ,Integer> map=new HashMap<>();
        for(int i=0;i<N;i++){
            String  n=b[i].split(" ")[0];
            int x=Integer.parseInt(b[i].split(" ")[1]);
            int y=Integer.parseInt(b[i].split(" ")[2]);
            int lenth= (int)Math.pow(x,2)+(int)Math.pow(y,2);
            map.put(n,lenth);
        }
        int max=map.get(b[0].split(" ")[0]);
        int min=map.get(b[0].split(" ")[0]);
        for(int i=1;i<N;i++){
            if(map.get(b[i].split(" ")[0])>=max){//找出最大距离
                max=map.get(b[i].split(" ")[0]);
            }
            if(map.get(b[i].split(" ")[0])<=min){//最小距离
                min=map.get(b[i].split(" ")[0]);
            }
        }

        System.out.println(getKey(map,min)+" "+getKey(map,max));

    }
    private static String  getKey(Map<String , Integer> map, int value){//根据值获取key
        String  key =null;
        for(String getKey: map.keySet()){
            if(map.get(getKey).equals(value)){
                key = getKey;
            }
        }
        return key;

    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值