我的代码不为输入1和1000或任何其他更大的输入运行 [英] my code does not run for the input 1 and 1000 or any other larger inputs

查看:131
本文介绍了我的代码不为输入1和1000或任何其他更大的输入运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

当我试图为输入1和1000运行此代码时,它会显示我的分段错误。 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 ++;
}
}

排序(sum,k);

cout<<< sum [k-1];
return 0;
}

无效排序(int sum [],long int k)
{
int i,j;
long int temp;
for(i = 0; i {
for(j = 0; j {
if i] <= sum [j])
{
temp = sum [i]
sum [i] = sum [j];
sum [j] = temp;
}
}
}

}


解决方案

分段错误是由堆栈溢出引起的。此行:

  int sum [100000]; 

sum 使用400K



要解决这个问题,你可以使用 std :: vector 请改用 sum


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 uses 400K spaces of stack, which is bigger than the normal size of stack.

To fix the problem, you can use std::vector to implement sum instead.

这篇关于我的代码不为输入1和1000或任何其他更大的输入运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆