fkcufk @ 2023-03-25 16:52:33
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,x;
double ans=0;
cin >> n;
for(int i=1;i<=n;i++){
cin >> x;
ans += x;
}
printf("%.2lf",ans/n);
return 0;
}
代码2(错误代码):
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,x;
double ans=0;
cin >> n;
while(n--){
cin >> x;
ans += x;
}
printf("%.2lf",ans/n);
return 0;
}
by 20120307zzy @ 2023-03-25 17:00:00
应为"
while(!n<=0){
cin>>x;
ans+=x;
n--;
}
by VainSylphid @ 2023-03-25 17:15:06
错误代码运行完n就变成0了,不能除以0
by L_zaa_L @ 2023-03-25 17:20:58
@BiiiiigApple 不对,应为while循环内n会被减到0,于是就求不出平均数
by fkcufk @ 2023-03-25 17:23:29
@zaa 谢谢二位大佬