pat 1108 Finding Average(20 分)

1108 Finding Average(20 分)

The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [1000,1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (100). Then N numbers are given in the next line, separated by one space.

Output Specification:

For each illegal input number, print in a line ERROR: X is not a legal number where X is the input. Then finally print in a line the result: The average of K numbers is Y where K is the number of legal inputs and Y is their average, accurate to 2 decimal places. In case the average cannot be calculated, output Undefined instead of Y. In case K is only 1, output The average of 1 number is Y instead.

Sample Input 1:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

Sample Output 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

Sample Input 2:

2
aaa -9999

Sample Output 2:

ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <string>
 6 #include <map>
 7 #include <stack>
 8 #include <vector>
 9 #include <queue>
10 #include <set>
11 #define LL long long
12 #define INF 0x3f3f3f3f
13 using namespace std;
14 const int MAX = 1e3;
15 
16 int n, cnt = 0;
17 double ans = 0.0;
18 char s[MAX], notlegal[105][MAX];
19 
20 bool my_is_ans()
21 {
22     int len = strlen(s), my_cnt = 0;
23 //    if (s[0] == '.' || s[len - 1] == '.') return false;
24     for (int i = 0; i < len; ++ i)
25     {
26         if (isalpha(s[i])) return false;
27         if (s[i] == '.')
28         {
29             my_cnt ++;
30             if (my_cnt >= 2) return false;
31             if (i <= len - 4) return false;
32         }
33     }
34     return true;
35 }
36 
37 int main()
38 {
39 //    freopen("Date1.txt", "r", stdin);
40     scanf("%d", &n);
41     for (int i = 1; i <= n; ++ i)
42     {
43         scanf("%s", &s);
44         double d;
45 
46         if (!my_is_ans() || sscanf(s, "%lf", &d)!= 1 || d < -1000 || d > 1000)
47         {
48             strcpy(notlegal[cnt ++], s);
49             continue;
50         }
51         ans += d;
52     }
53 
54     for (int i = 0; i < cnt; ++ i)
55         printf("ERROR: %s is not a legal number\n", notlegal[i]);
56     if (n - cnt == 0)
57         printf("The average of 0 numbers is Undefined\n");
58     else if (n - cnt == 1)
59         printf("The average of 1 number is %.2lf\n", ans);
60     else
61         printf("The average of %d numbers is %.2lf\n", n - cnt, ans / (n - cnt));
62     return 0;
63 }

 

posted @ 2018-09-05 18:25  GetcharZp  阅读(312)  评论(0编辑  收藏  举报