在循环的末尾获取细分故障
Getting Segmentation fault at the end of FOR loop
我在C 程序中的循环结束时会得到分割故障。
这是我解决CodeForces问题的解决方案(比赛已经结束)。
这是我的代码。
int main()
{
int n,x,i;
cin>>n>>x;
int a[n];
int num[100010] = {0};
long long int ans = 0;
int temp;
for(i=0;i<n;i++)
{
cout<<n<<endl;
cin>>temp;
cout<<"hello"<<endl;
a[i]=temp;
DEBUG(temp);
num[temp]++;
DEBUG(i);
}
for(i=0;i<n;i++)
{
int p = (x^a[i]);
if(p==a[i])
{
ans += num[a[i]]-1;
}
else
{
ans += num[p];
}
}
cout<<ans/2<<endl;
return 0;
}
我在此输入上遇到错误。
85 64800
20812 83722 93301 72100 80530 13151 28349 97347 96271 41926 5482 11948 14969 90929 13749 92741 22218 21400 31801 10359 85151 23766 12470 68911 71675 33258 99921 44095 92037 36792 17169 81178 59941 37670 12836 26619 84395 5247 40024 15103 88105 87155 94050 11878 94868 8533 71675 37910 59558 22652 61981 8920 25631 62466 3498 69017 3488 14376 88486 98762 38766 86800 6168 76705 38901 14818 51981 45686 14274 13704 67957 75111 54600 74833 67172 13908 35100 93393 2610 63043 69433 3968 80412 11293 5053
,因为您的某些输入超过数组的大小,其大小为100010
在第34行
处int p =(x^a [i]);
这是一个XOR操作,可以导致P大于100010。例如,您的输入接近100010,但比那小。当您使用64800 XOR时,您可以在该输入中进行一些0到1,从而使其大于100010。
操作x^a [i]可能会抛出大于10010的数字。尝试打印每个迭代的值并查看您拥有的值。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 使用std ::复制复制阵列时获取细分故障
- 在循环的末尾获取细分故障
- C 程序获取细分故障