问题描述
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main()
{
int a,query,in,n,b[n],sum[a];
sum[1]=0;
scanf("%d",&query);
for(a=1;a<=query;a++)
{
scanf("%d",&in);
for(n=1;n<=in;n++)
{
b[n]=1+7*(n-1)+6*(n-1)*(n-2)+(n-1)*(n-2)*(n-3);
sum[a]=sum[a]+b[n];
}
}
for(a=1;a<=query;a++)
{
printf("%d\n",sum[a]);
}
return 0;
}
我已经编写了在terminal中运行的代码.
I have made this code which is running in terminal .
但是在黑客等级中,它正在显示
But in hacker rank it is showing
输入(stdin)
2
2
5
您的输出(标准输出)
~ no response on stdout ~
预期产量
9
225
编译器消息
Segmentation Fault
现在我该怎么办才能解决问题.
Now what should I do to solve the problem .
推荐答案
您的变量未初始化.结果,您的程序会调用未定义行为.
Your variables are uninitialized. As a result your program invokes Undefined Behavior.
例如,您没有初始化n
,但是随后声明了int b[n]
.数组b
的大小是多少?没人知道,因为n
具有垃圾值.
For example you do not initialize n
, but you then declare int b[n]
. What is the size of array b
? Nobody really knows, since n
has a garbage value.
首先确定变量的值,然后开始编码.
Start by figuring out what the values of your variables should be, and then start coding.
数组索引从0开始,因此您的for循环看起来不太好.
Array indexing starts from 0, thus your for loops are not looking good.
更改此:
for(a=1;a<=query;a++)
对此:
for (a = 0; a < query; a++)
这篇关于〜在stdout上没有响应〜在C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!