A1108. Finding Average

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<cstdio>
 2 #include<iostream>
 3 #include<string.h>
 4 using namespace std;
 5 char num[100];
 6 const double INF = 100000000.0;
 7 double prase_(char num[]){
 8     int len = strlen(num);
 9     int pos = -1, cnt = 0;
10     int tag = 0;
11     for(int i = 0; num[i] != '\0'; i++){
12         if(!(num[i] >= '0' && num[i] <= '9' || num[i] == '.' || num[i] == '-')){
13             return INF;
14         }
15         if(num[i] == '.')
16             cnt++;
17         if(cnt > 1)
18             return INF;
19     }
20     if(num[0] == '-')
21         tag = 1;
22     for(pos = 0; num[pos] != '.' && num[pos] != '\0'; pos++);
23     int P = 1, ansL = 0, ansR = 0;
24     if(pos == '\0'){
25         for(int i = len - 1; i >= tag; i--){
26             ansL += (num[i] - '0') * P;
27             P *= 10;
28         }
29         if(tag == 1)
30             return ansL * -1.0;
31         else return ansL * 1.0;
32     }else{
33         if(len - pos - 1 > 2)
34             return INF;
35         P = 1; ansL = 0;
36         for(int i = pos - 1; i >= tag; i--){
37             ansL += (num[i] - '0') * P;
38             P *= 10;
39         }
40         P = 1; ansR = 0;
41         for(int i = len - 1; i > pos; i--){
42             ansR += (num[i] - '0') * P;
43             P *= 10;
44         }
45         double temp = ansR * 1.0;
46         for(int i = 0; i < len - pos - 1; i++){
47             temp *= 0.1;
48         }
49         if(tag == 1)
50             return ((double)ansL + temp) * -1.0;
51         else return (double)ansL + temp;
52     }
53 }
54 int main(){
55     int N, cnt = 0;
56     double sum = 0, temp = 0;
57     scanf("%d", &N);
58     for(int i = 0; i < N; i++){
59         scanf("%s", num);
60         temp = prase_(num);
61         if(temp > 1000.0 || temp < -1000.0){
62             printf("ERROR: %s is not a legal number\n", num);
63         }else{
64             sum += temp;
65             cnt++;
66         }
67     }
68     if(cnt > 1){
69         double prt = sum / (double)cnt;
70         printf("The average of %d numbers is %.2f\n", cnt, prt);
71     }else if (cnt == 0){
72         printf("The average of 0 numbers is Undefined\n");
73     }else if(cnt == 1){
74         double prt = sum / (double)cnt;
75         printf("The average of %d number is %.2f\n", cnt, prt);
76     }
77     cin >> N;
78     return 0;
79 }
View Code

 

posted @ 2018-03-02 13:09  ZHUQW  阅读(218)  评论(0编辑  收藏  举报