此题有较大的玄学,如果将cmp函数的默认返回值更改为true,则会出现最后一个测试用例的“段错误”。
在代码中以“我的天!!!玄学”标识出
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct Node{
int weight;
vector<int> child;
};
Node t[1010];
vector<vector<int> > serialStore;
vector<int> tmpSerial;
bool cmp(vector<int> a, vector<int> b){
if(a.empty() || b.empty()){
return a.empty();
}
int minL = min(a.size(), b.size());
for(int i = 0; i < minL; i++){
if(a[i] != b[i]) return a[i] > b[i]; // 关键在这里的大于号
}
return false; // 我的天!!玄学
}
// 测试cmp是否工作的函数
void sortSerial(){
vector<int> tmp;
tmp.push_back(10);tmp.push_back

博客揭示了PAT A1053编程题的一个神秘现象,即在C++实现的cmp函数中,默认返回值改为true会导致最后一个测试用例出现“段错误”。作者通过‘我的天!!!玄学’标记了这一不寻常的问题。
最低0.47元/天 解锁文章
6267





