【问题标题】:How to resolve runtime error "SIGSEGV" in codechef?如何解决 codechef 中的运行时错误“SIGSEGV”?
【发布时间】:2021-09-26 12:39:38
【问题描述】:

我在提交时遇到运行时错误和 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. 解决此特定问题的更好方法。

【问题讨论】:

    标签: c segmentation-fault


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,顺便说一句,我也试过 cin 但这不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多