-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1054.cpp
50 lines (48 loc) · 1.03 KB
/
1054.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>
#include <cstdlib>
#include <cctype>
using namespace std;
bool f(string str)
{
int k = 0;
int dk = 0;
for (int i = 0; i < str.length(); ++i)
{
// if (!isalnum(str[i]) && str[i] != '-') return false;
if (str[i] == '.')
{
k = 0;
dk++;
}
else k++;
}
// cout << dk << " " << k << endl;
return dk <= 1 && k <= 2;
}
int main()
{
// freopen("1054.in", "r", stdin);
int n;
cin >> n;
int count = 0;
float sum = 0;
for (int i = 0; i < n; ++i)
{
string str;
cin >> str;
if (f(str))
{
count++;
// cout << atof(str.data()) << " ";
sum += atof(str.data());
}
else
cout << "ERROR: " << str << " is not a legal number" << endl;
}
if (count != 0)
printf("The average of %d numbers is %.2f", count, sum / count);
else
printf("The average of 0 numbers is Undefined");
return 0;
}