本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军;谁差得最远,谁就是菜鸟。本题给出一系列弹洞的平面坐标(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<iostream>
#include<string>
using namespace std;
struct gun
{
string num;
int Ox, Oy, ans;
}people[10000];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> people[i].num >> people[i].Ox >> people[i].Oy;
}
gun max, min;
max.ans = people[0].Ox*people[0].Ox + people[0].Oy*people[0].Oy;
max.num = people[0].num;
min.ans = max.ans; min.num = max.num;
for (int i = 1; i < n; i++)
{
people[i].ans = people[i].Ox*people[i].Ox + people[i].Oy*people[i].Oy;
if (people[i].Ox*people[i].Ox + people[i].Oy*people[i].Oy < min.ans)
{
min = people[i];
}
if (people[i].Ox*people[i].Ox + people[i].Oy*people[i].Oy > max.ans)
{
max = people[i];
}
}
cout << min.num << " " << max.num;
return 0;
}
ps:博主能力有限,如果读者发现什么问题,欢迎私信或评论指出不足。欢迎读者询问问题,乐意尽我所能解答读者的问题。欢迎评论,欢迎交流。谢谢大家!