如何解决codechef中的运行时错误“SIGSEGV”?

时间:2021-07-18 15:24:28

标签: c segmentation-fault

我在提交时遇到运行时错误和 WA,我的代码非常基本,所以你可以查看它

问题:https://www.codechef.com/problems/PLAYFIT

解决方案(得到“SIGSEGV”):

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  //taking test cases
    int T=0;
    scanf("%d", T);
    
    while(T--){
      
      int N=0, diff=0, maxDif=-1, gi=0, gj=0;
      //taking N
      scanf("%d", N);
      //taking match 1 goals
      scanf("%d", gi);
      
      for(int i=1;i<N;i++){
        //taking consecutuve goals in match
        scanf("%d", gj);
        //calc diff
        diff = gj - gi;
        
        if (diff > maxDif){
          maxDif = diff;
        }
        gi = gj;
      }
      
      //output
      if(maxDif >0){
        //cout<<maxDif<<endl;
        printf("%d \n", maxDif);
      }else{
        //cout<<"UNFIT"<<endl;
        printf("UNFIT \n");
      }
    }
    
    return 0;
}

我期待的是

  1. 为什么会出现运行时错误的解决方案和解释/
  2. 解决这个特定问题的更好方法。

1 个答案:

答案 0 :(得分:1)

请阅读scanf man page。这:scanf("%d", T); 应该改为 scanf("%d", &T);

其他对 scanf() 的调用也有同样的问题。

相关问题