my code does not run for the input 1 and 1000 or any other larger inputs(我的代码不适用于输入 1 和 1000 或任何其他更大的输入)
问题描述
当我尝试为输入 1 和 1000 运行此代码时,它会显示分段错误.此代码中的更正是什么?
when i am trying to run this code for input 1 and 1000 it shows me segmentation fault .what will be the correction in this code ?
void sorting(int sum[],long int k);
int main() {
int sum[100000];
int L,R,i,j;
long int k=0;
cin>>L;
cin>>R;
for(i=L;i<=R;i++)
{
for(j=i;j<=R;j++)
{
sum[k]=i^j;
k++;
}
}
sorting(sum,k);
cout<<sum[k-1];
return 0;
}
void sorting(int sum[],long int k)
{
int i,j;
long int temp;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
if(sum[i]<=sum[j])
{
temp=sum[i];
sum[i]=sum[j];
sum[j]=temp;
}
}
}
}
推荐答案
由于堆栈溢出导致分段错误.这一行:
The segmentation fault is caused by stack overflow. This line:
int sum[100000];
sum
使用了400K的栈空间,比正常的栈空间大.
sum
uses 400K spaces of stack, which is bigger than the normal size of stack.
要解决这个问题,你可以使用 std::vector
来实现 sum
.
To fix the problem, you can use std::vector
to implement sum
instead.
这篇关于我的代码不适用于输入 1 和 1000 或任何其他更大的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我的代码不适用于输入 1 和 1000 或任何其他更大的输入


基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01