PAT B1082 射击比赛 (20分)

题目链接https://pintia.cn/problem-sets/994805260223102976/problems/994805260990660608

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

代码

#include <cstdio>
#include <cmath>

int main() {
	int n, id, x, y, win, lose;
	double max = 0, min = 101, temp;
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d%d%d", &id, &x, &y);
		temp = sqrt(x * 1.0 * x + y * 1.0 * y);
		if(temp > max) {
			lose = id;
			max = temp;
		}
		if(temp < min) {
			win = id;
			min = temp;
		}
	}
	printf("%04d %04d\n", win, lose);
	return 0;
}
发布了327 篇原创文章 · 获赞 12 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Rhao999/article/details/105194837