注意使用stoi,stof,isdigit
#include<bits/stdc++.h>
#include<unordered_set>
#define f(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define debug(x) cerr<<#x<<": "<<x<<endl;
using namespace std;
typedef long long ll;
bool check(string s, double &x) {
int id = 0;
if (s[id] == '-')id++;
int fg = 0, count = 0;
int sz = s.size() - 1;
while (id <= sz) {
if (s[id] == '.'&&fg)return false;//two p
if (s[id] == '.')fg++;
else if (!isdigit(s[id]))return false;
else {
if (fg)count++;
}
id++;
}
if (count > 2)return false;
x = stof(s);
if (x < -1000 || x>1000)return false;
return true;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
//io格式处理:stoi,stof
int n;cin >> n;
double sum = 0;
int num = 0;
f(i, 1, n) {
double x;
string s;cin >> s;
if (check(s, x))sum += x, num++;
else {
cout << "ERROR: ";
cout << s;
cout << " is not a legal number" << endl;
}
}
if (num) {
if (num == 1) {
printf("The average of 1 number is %.2lf\n", sum);
}
else {
printf("The average of %d numbers is %.2lf\n", num, sum/num);
}
}
else {
printf("The average of 0 numbers is Undefined\n");
}
return 0;
}