-
Notifications
You must be signed in to change notification settings - Fork 0
/
1054_average.cpp
62 lines (62 loc) · 1.19 KB
/
1054_average.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;
int n;
char str[101][101];
bool check(char s[]){
int i = 0;
if(s[0]=='-'){
i++;
}
for(;s[i]&&s[i]!='.';i++){
if(!isdigit(s[i])){
return false;
}
}
if(s[i]=='.'){
for(int j=i+1;s[j];j++){
if(!isdigit(s[j])||j-i>2){
return false;
}
}
}
double a = fabs(atof(s));
if(a>1000.0){
return false;
}
return true;
}
void solve(){
int ans = 0;
double sum = 0;
for(int i=0;i<n;i++){
if(check(str[i])){
ans++;
sum += atof(str[i]);
}else{
printf("ERROR: %s is not a legal number\n",str[i]);
}
}
if(ans){
if(ans==1){
printf("The average of 1 number is %.2lf\n",sum);
}else{
printf("The average of %d numbers is %.2f\n",ans,sum/ans);
}
}else{
printf("The average of 0 numbers is Undefined\n");
}
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",str[i]);
}
solve();
return 0;
}