#include <cstdio>
#include <cstring>
//写法不严谨,居然测试点全过了
int main(){
int n, count = 0; //count是合法数字数量
double temp, legalSum = 0; //legalSum是合法数字总和
char str[100]; //保存每个读取的字符串
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%s", str);
int len = strlen(str), dotCount = 0; //dotCount记录小数点的数量
bool isLegal = true; //默认合法
if((str[0] > '9' || str[0] < '0') && str[0] != '-') isLegal = false; //第一位只能是数字或者符号
int dotPosition = -1; //记录小数点位置,判断精度是否符合要求
for(int j = 1; j < len; j++){
if(str[j] > '9' || str[j] < '0'){ //只能是数字或者小数点,且小数点只能出现一次
if(str[j] == '.'){
if(dotCount == 0) dotCount++, dotPosition = j;
else isLegal = false;
}else isLegal =false;
}
}
if(dotPosition != -1 && len - dotPosition > 3) isLegal = false; //用小数点位置判断精度
if(isLegal){
sscanf(str, "%lf", &temp);
if(temp >= -1000 && temp <= 1000){ //是否在规定范围内
count++;
legalSum += temp;
}
else printf("ERROR: %s is not a legal number\n", str);
}else printf("ERROR: %s is not a legal number\n", str);
}
if(count == 0) printf("The average of 0 numbers is Undefined");
else if(count == 1) printf("The average of 1 number is %.2f", legalSum);
else printf("The average of %d numbers is %.2f", count, legalSum / count);
return 0;
}
09-02
1337
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-31
1921
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)