#include<string>
#include<sstream>
#include <cstdio>
using namespace std;
string in[101];
void fail(int i){
cout<<"ERROR: "<<in[i]<<" is not a legal number"<<endl;
}
float str_to_flo(string i)
{
stringstream ss;
float k;
ss << i;
ss >> k;
ss.str("");
return k;
}
int main(){
int i,j,n;
vector<float> out;
cin>>n;
for(i=0;i<n;i++) cin>>in[i];
for(j=0;j<n;j++){
int k,f,pos;
k=f=pos=0;
while(in[j].find('.',pos)!=string::npos){
int l;
l=in[j].length();
k+=1;
pos=in[j].find('.',pos);
if(k>1 || (l>3&&pos<(l-3)) ){
fail(j);
f=1;
break;
}
pos++;
}
if(f==1) continue;
for(i=0;i<in[j].length();i++){
if(in[j][i]>'9' || in[j][i]<'0' && in[j][i]!='.'){
if(i!=0||in[j][i]!='-'){
fail(j);
f=2;
break;
}
}
}
if(f==0){
float tt;
tt = str_to_flo(in[j]);
if(tt<=1000&&tt>=-1000) out.push_back(tt);
else fail(j);
}
}
if(out.size()>0){
float a(0);
for(i=0;i<out.size();i++) a+=out[i];
if(out.size()==1) printf("The average of 1 number is %.2f",a);
else printf("The average of %d numbers is %.2f",out.size(),a/out.size() );
}else cout<<"The average of 0 numbers is Undefined";
return 0;
}
PAT-A:1108
最新推荐文章于 2022-05-13 12:03:11 发布