簡體   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

我試圖找到用戶輸入的給定列表中 3 個數字之間差異的最大總和,但我不斷收到 SIGTSTP,我無法理解我的代碼在哪里越界。

這是我的代碼:

#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;
}

輸入給定:

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];
 }

進入循環時將i設置為零。 只要i小於 3,循環就不會退出。但是循環中的任何內容都不會改變i的值。 所以i將永遠小於 3,循環永遠不會退出。

學習進行一些基本的故障排除,例如使用調試器或添加日志記錄。

此外,此代碼中沒有注釋。 我不知道這個循環應該做什么,所以不能給你關於如何修復它的具體建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM