PAT(B) 1082 射击比赛(Java)

题目链接:1082 射击比赛 (20 point(s))

题目描述

本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军;谁差得最远,谁就是菜鸟。本题给出一系列弹洞的平面坐标(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

Java代码

/**********************************************************************************
Submit Time			Status		Score	Problem	Compiler		Run Time	User
7/30/2019, 20:57:04	Accepted	20		1082	Java (openjdk)	178 ms		wowpH
Case 3: 容易TLE,多提交几次就行
**********************************************************************************/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] id = new String[n];						// id
        int[] distance = new int[n];						// 距离的平方
        for (int i = 0; i < n; ++i) {
            String[] athletes = br.readLine().split(" ");	// 运动员id,x,y
            id[i] = athletes[0];							// 保存id
            int x = Integer.parseInt(athletes[1]);
            int y = Integer.parseInt(athletes[2]);
            distance[i] = x * x + y * y;					// 距离的平方
        }

        int first = 0, last = 0;							// 冠军和菜鸟下标
        for (int i = 1; i < n; ++i) {
            if (distance[i] < distance[first]) {			// 比当前冠军距离短
                first = i;
            } else if (distance[i] > distance[last]) {		// 比当前菜鸟距离长
                last = i;
            }
        }

        System.out.println(id[first] + " " + id[last]);
    }
}

提交结果

提交结果

posted @ 2019-07-30 21:15  wowpH  阅读(307)  评论(0编辑  收藏  举报