【发布时间】:2018-12-25 18:48:42
【问题描述】:
我的编译器没有任何错误,我得到了正确的结果。我也尝试过在线 C++98 编译器,它也可以在那里工作。但是当我在比赛服务器上检查程序时,它说编译失败。
谁能告诉我如何处理我的编译器或我的代码有什么问题?这是程序:
#include <stdio.h>
#include <algorithm>
using namespace std;
class P
{
public:
int t;
int l;
P();
P(int t, int l);
bool operator<(P next);
};
P::P()
{
this->t = 0;
this->l = 0;
}
P::P(int x, int y)
{
this->t = x;
this->l = y;
}
bool P::operator<(P next)
{
return this->l > next.l;
}
P a[110];
int main()
{
int z, n, x, y, tim = 0;
scanf("%d %d",&z,&n);
for(int i = 0; i < z; i++)
{
scanf("%d %d",&x,&y);
P b(x,y);
a[i] = b;
}
sort(a,a + z);
tim = max(a[0].l,a[0].t);
for(int i = 1; i <= z; i++)
{
tim += a[i - 1].l - a[i].l;
tim = max(a[i].t,tim);
}
printf("%d\n",tim);
}
【问题讨论】:
-
您是否从服务器收到任何错误消息?
-
试试
stdio.h->cstdio。一个符合 c++98 的编译器不需要提供stdio.h,但大多数仍然提供。 -
bool operator<(const P& next) const;? -
与您的问题无关,但您可能应该为您的数组访问添加一些边界检查(
z的值)。另请注意,您将访问索引z处的数组,该数组将被初始化为零(因为a是一个全局变量,如果它是本地的a[z]将是indeterminate)。跨度> -
谢谢@MatthieuBrucher!它是'bool operator