patest吧 关注:1,493贴子:1,702
  • 1回复贴,共1

乙级1082 射击比赛 提问

只看楼主收藏回复

代码如下,只能过第三个测试点。将代码中的线性查找方法,更换为排序后输出数组的首末元素,则答案正确。本地调试时,在1-10个比赛者,解唯一,编号瞎写的输入下,线性查找法和排序法输出完全相同。
所以请问可能是什么问题(喷血)
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct shooter
{
string ID;
int distance;
};//基础结构
int dis(int x,int y)
{
int distance = (x*x+y*y);
return distance;
}//距离计算
bool compare(shooter a,shooter b)
{
return (a.distance<b.distance);
}//比较距离,线性查找或是排序法的比较函数。
int main()
{
int N;cin>>N;
shooter * shooters = new shooter[N];
int x;int y;
for(int i=0;i<N;i++)
{
cin>>shooters[i].ID>>x>>y;
shooters[i].distance = dis(x, y);
}//使用动态数组记录数据,经检验,输入无溢出,无泄漏。
shooter max={"1234",-1};shooter min{"1234",INT_MAX};//线性查找初始化,若此处改为数组中的任一元素,可以过第一个测试点。
for(int i=0;i<N;i++)
{
if(compare(max,shooters[i]))
max=shooters[i];
if(compare(shooters[i],min))
min = shooters[i];
}
cout<<max.ID<<' '<<min.ID;//整段代码删掉换成sort(shooters,shooters+N,compare),答案即正确。
delete []shooters;
}


IP属地:浙江1楼2018-08-02 23:34回复
    我写的 https://blog.csdn.net/chenyvye/article/details/79708647


    IP属地:广东来自Android客户端2楼2018-08-06 14:05
    回复