[英]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.