solution
测试点2:合法数据只有一个是,number单数
The average of 1 number is Y
#include<iostream>
#include<string>
using namespace std;
bool judge(string s){
int point = 0, cnt = 0, flag = 0;
for(int i = 0; i < s.size(); i++){
if(s[i] == '.'){
if(!point) point = 1;
else return false;
}
else if(isdigit(s[i])){
if(point){
cnt++;
if(cnt > 2) return false;
}
}
else if(s[i] == '-' && !flag) flag = 1;
else return false;
}
if(stod(s) < -1000 || stod(s) > 1000) return false;
return true;
}
int main(){
string s;
double n, sum = 0, k = 0, t;
cin >> n;
while(n--){
cin >> s;
if(!judge(s)) cout << "ERROR: " << s << " is not a legal number" << endl;
else{
k++;
sum += stod(s);
}
}
if(!k) cout << "The average of 0 numbers is Undefined";
else if(k == 1) printf("The average of %.0f number is %.2f", k, sum / k);
else printf("The average of %.0f numbers is %.2f", k, sum / k);
return 0;
}