PTA basic 1082 射击比赛 (20 分) c++语言实现(g++)

本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军;谁差得最远,谁就是菜鸟。本题给出一系列弹洞的平面坐标(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 <vector>
#include <algorithm>
#include <string>
#include <math.h>
#include <map>
using namespace std;
class athlate{
public:
    string id;
    int x;
    int y;
    double dfz;//distence from zero
    athlate()=default;
    athlate(string id_,int x_,int y_):id{id_},x{x_},y{y_}{
        double result=x*x+y*y;
        dfz=sqrt(result);
    };
};
bool compare(athlate l,athlate r){
    return (l.dfz-r.dfz)>0;//true 位置不变 false 位置互换  左边大于右边时 位置不变 运算结束后 第一个是最大值 最后一个是最小值
}

int main(){
    int n,count{0};
    string str;
    int x,y;
    cin>> n;
    map<string,int> strmap;
    vector<athlate> alist;
    for(int i=0;i<n;i++){
        cin >> str >> x >> y;
        x=abs(x);
        y=abs(y);
        strmap[str]=count++;
        alist.push_back(athlate{str,x,y});
    }
    sort(alist.begin(),alist.end(),compare);
    cout << alist.back().id <<" "<<alist[0].id<<endl;
    return 0;
}

 

posted @ 2021-05-13 19:33  keiiha  阅读(97)  评论(0编辑  收藏  举报