Segmentation fault:段错误,检查是否有数组越界,指针异常,访问到不应该访问的内存 10

运行会放出上面的提示#include<stdio.h>main(){intn,i,t,j=1;doubleavg1,avg2,a[100],s=0,v=0;while(s... 运行会放出上面的提示
#include<stdio.h>
main()
{
int n,i,t,j=1;
double avg1,avg2,a[100],s=0,v=0;
while(scanf("%d",&n) !=EOF)
{ s=0;
j=1;
t=n;
for(i=0;i<n;i++){
scanf("%lf",&a[i]);
s=s+a[i];
}
avg1=s*1.0/n;
while(j!=n){
for(i=j;i<n;i++){
v=v+a[i];
}

题目下面
题目描述
Given N numbers in a line, we can determine a continuous subsequence by giving its start position and its length.
PMH and Roy played a game the other day. Roy gives the start position first, then PMH gives the length. Roy wants the mean of the subsequence as large as possible, while PMH wants it as small as possible.
You are to calculate the best result Roy can get, assuming PMH is very clever.
输入
There are multiple testcases.

Each testcase begins with a line containing N only.

The following line contains N numbers, separated by spaces.
输出
For each testcase, you are to print the best mean of subsequece Roy can get, precise to 6 digit after decimal point.
样例输入
10
2 10 4 6 5 10 10 2 3 2
样例输出
5.777778
avg2=v*1.0/(n-j);

if(avg2>avg1){
avg1=avg2;
}
v=0;
j++;
}
printf("%.6lf\n",avg1);

}

}
#include<stdio.h>
main()
{
int n,i,t,j=1;
double avg1,avg2,a[100],s=0,v=0;
while(scanf("%d",&n) !=EOF)
{ s=0;
j=1;
t=n;
for(i=0;i<n;i++){
scanf("%lf",&a[i]);
s=s+a[i];
}
avg1=s*1.0/n;
while(j!=n){
for(i=j;i<n;i++){
v=v+a[i];
}
avg2=v*1.0/(n-j);
if(avg2>avg1){
avg1=avg2;
}
v=0;
j++;
}
printf("%.6lf\n",avg1);
}
}
这是我的程序 上面有点不全
展开
 我来答
天天颖颖
2017-05-05 · TA获得超过904个赞
知道小有建树答主
回答量:565
采纳率:50%
帮助的人:262万
展开全部
如果输入n大于100,就会出错。数组a会溢出
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式