【发布时间】:2017-07-19 07:15:52
【问题描述】:
因此,我在在线法官上提交了此代码,而代码在我的系统上运行良好,但在线法官上的相同输入却给了我这个错误。
GDB trace:
Reading symbols from solution...done.
[New LWP 4622]
Core was generated by `solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl (
this=0x7ffe815275a0) at /usr/include/c++/7/bits/stl_vector.h:89
89 : _Tp_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage()
#0 std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl (
this=0x7ffe815275a0) at /usr/include/c++/7/bits/stl_vector.h:89
#1 std::_Vector_base<int, std::allocator<int> >::_Vector_base (
this=0x7ffe815275a0) at /usr/include/c++/7/bits/stl_vector.h:127
#2 std::vector<int, std::allocator<int> >::vector (this=0x7ffe815275a0)
at /usr/include/c++/7/bits/stl_vector.h:263
#3 main () at solution.cc:8
由于比赛正在进行,我不确定是否要分享我的代码,但如果有人对此错误有所了解,那将非常有帮助。
请注意,该错误是由 Cpp 14 上完全相同的输入引起的。
好的,所以我决定分享我的代码:
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n],b[n];
int max=1000000;
vector<int> c1[max+1],c2[max+1];
for(int i=0;i<n;i++){
cin>>a[i];
c1[a[i]].push_back(a[i]);
}
for(int i=0;i<n;i++){
cin>>b[i];
c2[b[i]].push_back(b[i]);
}
for(int i=max;i>=1;i--){
//cout<<i<<endl;
int f1=0,f2=0;
for(int j=i;j<=max;j+=i){
if(c1[j].size()>0){
f1=1;
break;
}
}
for(int j=i;j<=max;j+=i){
if(c2[j].size()>0){
f2=1;
break;
}
}
if(f1 && f2){
int max1=0,max2=0;
for(int j=i;j<=max;j+=i){
if(c1[j].size()){
for(int k=0;k<c1[j].size();k++){
if(max1<c1[j][k])
max1=c1[j][k];
}
}
}
for(int j=i;j<=max;j+=i){
if(c2[j].size()){
for(int k=0;k<c2[j].size();k++){
if(max2<c2[j][k])
max2=c2[j][k];
}
}
}
cout<<max1+max2;
return 0;
}
}
}
既然这被否决了,我只想知道为什么?我怎样才能更好地提出这个问题?
【问题讨论】:
-
这个“在线判断”会生成可下载的调试符号和核心转储吗?
-
@zv_ 没有生成调试符号。
-
这将有助于发布您的相关代码,甚至可以进行更改以找出问题所在。
-
@Maikel 分享了它
-
vector<int> c1[max+1],c2[max+1];显然是错误的,并且不是您想要的。请改用vector<int> c1(max+1); vector<int> c2(max+1);。
标签: c++ runtime-error c++14