#include<iostream>
#include<vector>
#include<string>
using namespace std;
bool legal(string str)
{
if (str[0] == '-') str.erase(0, 1);
int pos = str.find('.');
if (pos<str.size()&&(str.size() - pos - 1 > 2 || pos >= 5)) return false;//有小数点的情况
for (unsigned t = 0;t < str.size();t++)
if (t != pos)
if (str[t]<'0' || str[t]>'9') return false;
if (stod(str) > 1000) return false;
return true;
}
int main()
{
int cnt=0;
double sum=0;
int N;
cin >> N;
while (N--)
{
string str;
cin >> str;
if (legal(str)) {
cnt++;
sum += stod(str);
}
else
printf("ERROR: %s is not a legal number\n", str.c_str());
}
if(cnt==1) printf("The average of %d number is %.2f\n", cnt, sum / cnt);
else if(cnt==0) printf("The average of %d numbers is Undefined\n", cnt);
else printf("The average of %d numbers is %.2f\n", cnt, sum / cnt);
}
1108. Finding Average (20)
最新推荐文章于 2017-09-10 19:34:31 发布