【发布时间】:2017-09-14 14:00:15
【问题描述】:
在使用大量数据(大约 2MB)执行以下代码时,我在第 21 行收到一个浮点异常。
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
int S, Q, type, seq, pos;
int x, y, lastAnswer = 0;
cin >> S >> Q;
vector< vector <int> > seqList(S);
for(int i=0; i<Q; i++){
cin >> type >> x >> y;
if( type == 1 ){
seq = (x^lastAnswer) % S;
seqList[seq].push_back(y);
if(seq == 0) lastAnswer = 0;
}
else if( type == 2 ){
seq = (x^lastAnswer) % S;
pos = (int)(y % seqList[seq].size()); //Line 21
lastAnswer = seqList[seq][pos];
cout << lastAnswer << endl;
}
}
return 0;
}
【问题讨论】:
-
那是因为 size 为 0。而 don't #include <bits/stdc++.h> 和 don't use
using namespace std -
您使用的是哪个编译器(和版本)?将 "floating point" 命名为不涉及任何浮点类型的操作引发的异常确实具有误导性。
-
您知道
^运算符是按位的exclusive or 运算符吗?
标签: c++ error-handling floating-point