题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805360777347072
基本一遍过,题意理解有点问题
注意count = 1时,输出的是number
代码
#include <cstdio>#include <string>#include <iostream>using namespace std;bool judge(string str){int dotcount = 0;for(int i = 0; i < str.size(); i++){if(!((str[i] >= '0' && str[i] <= '9')|| str[i] == '.' || str[i] == '-')){return false;}else if(str[i] == '.'){if(str.size() - i > 3) return false;dotcount++;if(dotcount > 1) return false;}}if(stod(str) > 1000.0 || stod(str) < -1000.0) return false;return true;}int main(){int n, count = 0;double sum = 0.0;string input;scanf("%d", &n);for(int i = 0; i < n; i++){cin>>input;if(judge(input)){count++;sum += stod(input);} else {cout<<"ERROR: "<<input<<" is not a legal number"<<endl;}}if(count == 0){printf("The average of 0 numbers is Undefined");} else if(count == 1){printf("The average of 1 number is %.2f", sum/count);}else {printf("The average of %d numbers is %.2f", count, sum/count);}return 0;}
