本題目給出的射擊比賽的規則非常簡單,誰打的彈洞距離靶心最近,誰就是冠軍;誰差得最遠,誰就是菜鳥。本題給出一系列彈洞的平面座標(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:博主能力有限,如果讀者發現什麼問題,歡迎私信或評論指出不足。歡迎讀者詢問問題,樂意盡我所能解答讀者的問題。歡迎評論,歡迎交流。謝謝大家!