题目描述
本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军;谁差得最远,谁就是菜鸟。本题给出一系列弹洞的平面坐标(x,y),请你编写程序找出冠军和菜鸟。我们假设靶心在原点(0,0)。
输入格式:
输入在第一行中给出一个正整数 N(≤ 10 000)。随后 N 行,每行按下列格式给出:
其中 ID 是运动员的编号(由 4 位数字组成);x 和 y 是其打出的弹洞的平面坐标(x,y),均为整数,且 0 ≤ |x|, |y| ≤ 100。题目保证每个运动员的编号不重复,且每人只打 1 枪。
输出格式:
输出冠军和菜鸟的编号,中间空 1 格。题目保证他们是唯一的。
输入样例:
3
0001 5 7
1020 -1 3
0233 0 -1
输出样例:
0233 0001
代码示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int numbers = Reader.nextInt();
String lowerId = "";
String winerId = "";
// 距离越大越差劲
int lowerDistance = Integer.MIN_VALUE;
int winerDistance = Integer.MAX_VALUE;
for (int i = 0; i < numbers; i++) {
String id = Reader.next();
int x = Reader.nextInt();
int y = Reader.nextInt();
int distance = x * x + y * y;
// 如果距离比最差的还要远,就要替换目前最远的
if (distance > lowerDistance) {
lowerId = id;
lowerDistance = distance;
}
// 如果距离比最厉害的还要近,就要替换目前最近的
if (distance < winerDistance) {
winerId = id;
winerDistance = distance;
}
}
// 输出结果
System.out.println(winerId + " " + lowerId);
}
}
/**
* 更快的一个输入输出类
*/
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while (!tokenizer.hasMoreTokens())
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}