~ 在标准输出上没有响应 ~ 在 C 中 [英] ~ no response on stdout ~ in C
问题描述
#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
",sum[a]);
}
return 0;
}
我已经制作了在终端中运行的代码.
I have made this code which is running in terminal .
但在黑客排名中显示
输入(标准输入)
2
2
5
你的输出(标准输出)
~ no response on stdout ~
预期输出
9
225
编译器消息
Segmentation Fault
现在我应该怎么做才能解决这个问题.
Now what should I do to solve the problem .
推荐答案
您的变量未初始化.因此,您的程序会调用 Undefined Behavior.
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++)
这篇关于~ 在标准输出上没有响应 ~ 在 C 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!