#include<bits/stdc++.h>
using namespace std;
bool judge(string a){
int ansnot=0;int flag=0;int ansnotafter=0;
for(int i=0;i<a.length();i++){
if(a[i]<='z'&&a[i]>='a') return false;
if(flag==1) ansnotafter++;
if(a[i]=='.'){
flag=1;
ansnot++;
}
if(ansnot>1) return false;
if(ansnotafter>2) return false;
}
return true;
}
double getnum(string a){
int flag=0;
if(a[0]=='-'){
flag=1;a=a.substr(1,a.length());
}
double ans=0;int notflag=0;int j=1;
for(int i=0;i<a.length();i++){
if(notflag==1){
ans=ans+(a[i]-'0')*(pow(10,(0-j)));j++;
}else{
if(a[i]<='9'&&a[i]>='0')
ans=ans*10+(a[i]-'0');
}
if(a[i]=='.'){
notflag=1;continue;
}
}
if(flag==1) return 0-ans;
else return ans;
}
int main()
{
freopen("in.txt","r",stdin);
int n;cin>>n;double ans=0;int cnt=0;
for(int i=0;i<n;i++){
string temp;cin>>temp;
if(judge(temp)==true){
if(getnum(temp)>1000||getnum(temp)<-1000){
cout<<"ERROR: "<<temp<<" is not a legal number"<<endl;cnt++;
}else{
ans+=getnum(temp);
}
}else{
cout<<"ERROR: "<<temp<<" is not a legal number"<<endl;cnt++;
}
}
if(cnt==n){
cout<<"The average of 0 numbers is Undefined"<<endl;
}else if(cnt==n-1){
cout<<"The average of 1 number is ";
printf("%.2lf\n",ans);
}else{
cout<<"The average of "<<n-cnt<<" numbers is ";
printf("%.2lf\n",ans/((n-cnt)*1.0));
}
}
虽然情况有很多种,但实际上只要考虑题中的几个情况并加以限制就可以了...