如何解决 codechef 中的运行时错误 "SIGSEGV"?
How to resolve runtime error "SIGSEGV" in codechef?
我在提交时遇到了运行时错误和 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;
}
我期待的是
- 出现运行时错误的解决方案和解释/
- 解决这个特定问题的更好方法。
请阅读scanf
man page。这:scanf("%d", T);
应该是 scanf("%d", &T);
。
其他对scanf()
的调用也有同样的问题。
我在提交时遇到了运行时错误和 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;
}
我期待的是
- 出现运行时错误的解决方案和解释/
- 解决这个特定问题的更好方法。
请阅读scanf
man page。这:scanf("%d", T);
应该是 scanf("%d", &T);
。
其他对scanf()
的调用也有同样的问题。