简体   繁体   English

我的代码不断收到 SIGTSTP 错误,但我无法弄清楚我的代码在哪里越界

[英]I keep getting SIGTSTP error for my code but I am not able to figure out where is my code going out of bounds

I am trying to find the largest sum for the difference between 3 numbers in a given list inputed by the user but I keep getting SIGTSTP and I am not able to understand where my code in going out of bounds.我试图找到用户输入的给定列表中 3 个数字之间差异的最大总和,但我不断收到 SIGTSTP,我无法理解我的代码在哪里越界。

Here is my code:这是我的代码:

#include <iostream>
#include<vector>
#include <algorithm>
using namespace std;

int main() {
    // your code goes here
    int t,n,z;
    vector<long long int> a;
    long long int max,min,k,x,sum;
    cin>>t;
    while(t--){
        cin>>n;
        while(n--){
            cin>>x;
            a.push_back(x);
        }
     max = *max_element(a.begin(),a.end());
     min = *min_element(a.begin(),a.end());
    
     int i=0,k=0;
     //to find a element other than minimum or maximum element and assign it to k
     while(i<3){
         if(a[i]==max || a[i]==min)
           continue;
         else
          k=a[i];
        i++;
     }
     if(k==0)
      k=a[0];
     //sum
     sum = abs(max-min) + abs(k-min) + abs(max-k);
     cout<<sum;
    }
    return 0;
}

Input given:输入给定:

3
3
2 7 5
3
3 3 3
5
2 2 2 2 5
 int i=0,k=0;
 while(i<3){
     if(a[i]==max || a[i]==min)
       continue;
     else
      k=a[i];
 }

You set i to zero when you enter the loop.进入循环时将i设置为零。 The loop will not exit so long as i is less than 3. But nothing in the loop changes the value of i .只要i小于 3,循环就不会退出。但是循环中的任何内容都不会改变i的值。 So i will always be less than 3 and the loop will never exit.所以i将永远小于 3,循环永远不会退出。

Learn to do some basic troubleshooting such as using a debugger or adding logging.学习进行一些基本的故障排除,例如使用调试器或添加日志记录。

Also, there are no comments in this code.此外,此代码中没有注释。 I have no idea what this loop is supposed to do, so can't give you specific suggestions on how to fix it.我不知道这个循环应该做什么,所以不能给你关于如何修复它的具体建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我执行了以下代码并且我的系统卡住了,但我无法找出背后的原因? - I executed the following code and my system stuck, but I am not able to figure out reason behind it? 我的程序不断抛出编译错误,我无法弄清为什么会发生该错误 - My program keeps throwing compilation error,I am not able to figure out why the error has occured 我无法找出这段代码哪里出错了 - I am Not able to find out where this code went wrong 谁能帮我弄清楚为什么我的lValue总是出错 - Can anyone help me figure out why I keep getting an error with my lValue 我不明白为什么我的代码出现段错误 - I can't figure out why I'm getting a seg fault in my code 为什么我的程序继续给我堆缓冲区溢出,即使我没有超出我的数组或覆盖任何值 - Why does my program keep on giving me heap buffer overflow even though I am not going out of bounds on my array or overwriting any values "我的代码中存在对齐问题和额外间距,但我不知道在哪里?" - There is an Alignment Issue and Extra Spacing in my code, but I cannot figure out where? 我想不出让我的代码循环的方法 - I cant figure out a way to make my code loop 我无法弄清楚我的代码有什么问题 - I can't figure out what's wrong with my code 这是我被卡住的问题。 在 22 个测试用例中,有 5 个性能测试用例失败了。 如何优化我的代码? - This is a problem where I am stuck. Out of 22 test cases 5 performance test cases are getting failed. How to optimise my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM