-
Notifications
You must be signed in to change notification settings - Fork 0
/
1054-2.cpp
50 lines (46 loc) · 1.11 KB
/
1054-2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <cstring>
#include <math.h>
using namespace std;
bool mydigit(char a) {
return !(isdigit(a) || a == '.');
}
bool isLegal(string s) {
int point = count(s.begin(),s.end(),'.'); //记录小数点
int m = 0; //记录小数位数
if (s.find('.') != string::npos) m = s.length() - s.find('.') - 1;
if (!(s[0] == '-'&&s.length()>1) && !isdigit(s[0])) //第一位必为数字或者负号(不能只有负号)
return 0;
if (m <= 2 && fabs(stof(s)) <= 1000 && find_if(s.begin()+1, s.end(), mydigit) == s.end()) //必须保证在题目范围内
return 1;
return 0;
}
int main()
{
int n;
cin>>n;
string s;
double sum=0;
double num=0;
while(n--)
{
cin>>s;
if(isLegal(s))
{
sum=sum+stof(s.c_str());
num++;
}
else
cout<<"ERROR: "<<s<<" is not a legal number"<<endl;
}
if(num>1)
printf("The average of %.f numbers is %.2f\n",num,sum/num);
else if(num==1)
printf("The average of 1 number is %.2f\n",sum);
else
printf("The average of 0 numbers is Undefined\n");
return 0;
}