#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=100005;
bool judge(char s[])
{
int len=strlen(s);
int i;
int t1=0,t2=0,t3=0;
for(i=0;i<len;i++)
{
if(s[i]=='-')
t1++;
else if(s[i]=='.')
t2++;
else if(s[i]>='0'&&s[i]<='9')
t3++;
}
if(t1+t2+t3<len)
return 0;
//只剩三种字符
if(t3==0)
return 0;
if(t1>1)
return 0;
if(t2>1)
return 0;
//保证- .都最多只有一个
for(i=1;i<len;i++)
{
if(s[i]=='-')
return 0;
}
//保证若有-则必在第一个
int j;
for(j=0;j<len;j++)
{
if(s[j]=='.')
break;
}
//已经是数字了
if(len-(j+1)>2)
return 0;
//小数点后面控制完毕
// if(s[0]=='.'||(s[0]=='-'&&s[1]=='.'))
// return 0;
return 1;
}
int main()
{
// freopen("in.txt","r",stdin);
int N;
int cou=0;
double sum=0;
scanf("%d",&N);
for(int i=0;i<N;i++)
{
char s[maxn];
scanf("%s",s);
if(judge(s)==0)//不是数字
{
printf("ERROR: %s is not a legal number\n",s);
continue;
}
else
{
double t;
sscanf(s,"%lf",&t);
if(t>1000||t<-1000)
{
printf("ERROR: %s is not a legal number\n",s);
continue;
}
else
{
// printf("%s\n",s);
cou++;
sum+=t;
}
}
}
if(cou==0)
printf("The average of 0 numbers is Undefined\n");
else if(cou==1)
printf("The average of %d number is %.2f",cou,sum/cou);
else
printf("The average of %d numbers is %.2f",cou,sum/cou);
return 0;
}
1108. Finding Average (20)
最新推荐文章于 2021-08-21 18:04:56 发布