xcjl124596XCJL @ 2024-05-18 20:45:44
#include<stdio.h>
int main(){
int n;
int a[n];
int i,t;
double b,e;
b=0;
t=0;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",a+i);
}
for(i=0;i<n;i++){
t=a[i];
b=(double)(b+t);
}
e=(double)b/n;
printf("%.2lf\n",e);
return 0;
}
by HAha20120522 @ 2024-05-18 20:50:47
数组定义是必须开固定大小。s
by HAha20120522 @ 2024-05-18 20:52:35
@xcjl124596XCJL
const int N = 1e2 + 5;
int a[N];
这样才能编译通过。
by HAha20120522 @ 2024-05-18 20:53:40
话说你那是啥编译器啊。
by pigeonteam @ 2024-05-18 20:54:54
@HAha201205221633 应该楼主说的是样例没问题,DEV用的应该是gcc4.9或者9.3(两个版本)
by uuplayer @ 2024-06-10 22:09:33
@xcjl124596XCJL 有时候呢,运行对了不代表代码对了,还是需要多开一些测试样例试试,才能验证程序是否完全正确
by 0x3E @ 2024-07-06 22:04:31
直接这样写就可以,你的有点多余了:
#include <stdio.h>
using namespace std;
int main() {
int n, i;
double sum = 0;
scanf("%d", &n);
for (i = 1; i <= n; i ++) {
int a;
scanf("%d", &a);
sum += a;
}
printf("%.2lf", sum/n);
return 0;
}
其实不用开数组,循环时每次定义一次就行了
by MATTCZJ @ 2024-07-24 18:27:57
屎
by zhuzihan0903 @ 2024-07-25 12:54:47
@xcjl124596XCJL 编译器肯定是GCC吧
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
double a[n],sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
sum+=a[j];
}
sum/=n;
cout<<fixed<<setprecision(2)<<sum;
return 0;
}