# B-1054 average value (20 points)

Posted by summoner on Tue, 08 Feb 2022 20:12:48 +0100

1054 average (20 points)

The basic requirement of this problem is very simple: given N # real numbers, calculate their average value. But the complication is that some input data may be illegal. A "legal" input is a real number in the [− 10001000] range and can be accurate to 2 decimal places at most. When you calculate the average, you can't include those illegal data.

### Input format:

Enter the first line to give a positive integer N (≤ 100). The next line gives # N # real numbers separated by a space.

### Output format:

For each illegal input, output ERROR: X is not a legal number in one line, where x is the input. Finally, output the result in one line: The average of K numbers is Y, where  is the number of legal inputs, and Y  is their average value, accurate to 2 decimal places. If the average cannot be calculated, replace # y with # Undefined #. If ， K ， is 1, then ， The average of 1 number is Y is output.

### Input example 1:

```7
5 -3.2 aaa 9999 2.3.4 7.123 2.35```

### Output example 1:

```ERROR: aaa is not a legal number
ERROR: 9999 is not a legal number
ERROR: 2.3.4 is not a legal number
ERROR: 7.123 is not a legal number
The average of 3 numbers is 1.38```

```2
aaa -9999```

### Output example 2:

```ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined```

This question is not difficult, but it is very detailed. It is annoying to realize regularity by yourself, but it is not difficult. It is stuck in the third test point. I always wondered what the meaning of the special judgment "if K ＾ is 1, then output ＾ The average of 1 number is Y" was. There was no special judgment. I stuck for a long time, and then I found the big pit of not reading the question carefully

Pit:
"If K is 1, then output The average of 1 number is Y" number becomes singular, missing s ， even stuck in the reading question

### Code 1: (it's troublesome to realize regularization by yourself)

```#include<bits/stdc++.h>
using namespace std;
bool judge(string str,double &d){
string regx="+-1234567890.";//Legal numbers can only contain these characters, and the + - sign can only appear at the beginning Up to one
if(str.find_first_not_of(regx)!=-1) return false;//Illegal characters
if((int)str.find_last_of("-")>0) return false;//(only - 1 or 0) there is + - but not at the beginning
if(str.find_first_of('.')!=str.find_last_of('.')) return false;//There are multiple
int n=str.find_last_of('.');//The legal numbers have been screened out
if(n!=-1&&n+3<str.length()) return false;//Legal number, but greater than 2 digits after the decimal point
sscanf(str.c_str(),"%lf",&d);//str->double
return d>=-1000&&d<=1000;//Withstand the screening, and the rest is legal
}
int main(){
//	freopen("in.txt","r",stdin);
int N,num=0;
double sum=0.0,d;
string str;
cin>>N;
while(N--){
cin>>str;
if(!judge(str,d)) cout<<"ERROR: "<<str<<" is not a legal number\n";
else { num++,sum+=d;}
}
if(num==0) cout<<"The average of 0 numbers is Undefined";
else if(num==1) printf("The average of 1 number is %.2f",sum);//number is less than s
else printf("The average of %d numbers is %.2f",num,sum/num);
return 0;
}```

### Code 2: ★ (I found some clever practices of big guys when checking bug s. sscanf also has its own exception handling mechanism) (can use api)

First sscanf() string - > double ， and then sprintf (%. 2f) double - > string ， after going back and forth, if the first two strings meet the prefix of the former and the latter, it can be determined that they are legal numbers
Because:
1. sprintf can format the output%. 2f without judging the decimal places
2. If aaa cannot be read, it is equivalent to not reading. d keeps the original value. After converting the string, the "legal number" must not be equal to "aaa"

Observe the relationship before and after conversion:

5       5.00
-3.2    -3.20
aaa     -3.20
9999    9999.00
2.3.4   2.30
7.123   7.12
2.35    2.35

Legal input (the size range is not considered temporarily) meets: the former is the prefix of the latter

### code:

```#include<bits/stdc++.h>
using namespace std;
int main(){
//	freopen("in.txt","r",stdin);
int N,num=0;
double sum=0,de;
char str1[100],str2[100];
cin>>N;
while(N--){
cin>>str1;
sscanf(str1,"%lf",&de);//STR1 - > double stops automatically when an illegal character is encountered
sprintf(str2,"%.2lf",de);//Double - > STR2 is a legal number
bool flag=0;
for(int i=0;i<strlen(str1);i++) if(str1[i]!=str2[i]) flag=1;//It doesn't matter whether str1 is the prefix of str2. str1 is too long. str2 length is 100 '\ 0'! = 'number'
if(flag||de>1000||de<-1000)
cout<<"ERROR: "<<str1<<" is not a legal number\n";
else{num++,sum+=de;};
}
if(num==0) cout<<"The average of 0 numbers is Undefined";
else if(num==1) printf("The average of 1 number is %.2f",sum);//number is less than s
else printf("The average of %d numbers is %.2f",num,sum/num);
return 0;
}```

This code has an interesting phenomenon: sometimes the answer is correct and sometimes the runtime is wrong. Need to submit several more times, fan code Mystery OJ