测试点3应该是类似于大于1000然后还有个小数点的,例如:1001.
按照题目要求.42或者是42.都是合法的
我原来判断小数点都默认把带有小数点的树都在合法范围内,没有判断是不是在[-1000,1000]内
测试点三就一直过不去😂;
#include <bits/stdc++.h>
using namespace std;
bool judge (string s) {
int cntClum = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] >= 65 && s[i] <= 122)
return false;
if (s[i] == '.') cntClum++;
}
if (cntClum > 1) return false;
int pos = s.find('.');
if (pos < s.size()) {
if (s.size() - pos > 3) return false;
double temp = stod(s);
if (temp > 1000 || temp < -1000) { //坑就在这
return false;
}
}
else {
double temp = stod(s);
if (temp > 1000 || temp < -1000) {
return false;
}
}
return true;
}
int main() {
int n, cnt = 0;
string s;
double sum = 0.0, temp;
scanf ("%d", &n);
for (int i = 0; i < n; i++) {
cin >> s;
if (judge(s)) {
temp = stod(s);
sum += temp;
cnt++;
}
else {
printf ("ERROR: %s is not a legal number\n", s.c_str());
}
}
if (cnt == 0) printf ("The average of 0 numbers is Undefined");
else if (cnt == 1) printf ("The average of %d number is %.2f", cnt, sum / (1.0 * cnt));
else printf ("The average of %d numbers is %.2f", cnt, sum / (1.0 * cnt));
}