PAT 1054 averaging (20 minutes)

Averaging 1054 (20 minutes)

The basic requirements of this problem is simple: for a given N real numbers, their average is calculated. But complication is that some of the input data may be illegal. A "legitimate" is a real number in the input [-1000,1000] interval, and at most accurate to one decimal place. When you calculate the average, not those illegal data count.

Input formats:

Input of the first row is given a positive integer N (≦ 100). Then given line N real numbers, separated by spaces between a number.

Output formats:

, The output for each row in the illegal input ERROR: X is not a legal number, which Xis input. Line with the final output: The average of K numbers is Ywhere Kis the number of legal input, Yis an average value thereof, two decimal place. If the average value can not be calculated, with the Undefinedreplacement Y. If K1, then output The average of 1 number is Y.

Sample Input 1:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35


      
    

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


      
    

Output Sample 2:

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

Note: read a lot of blog and found that test point 3 actually, when qualifying number is number 1 instead of numbers

AC Code

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int N, n = 0;
    cin >> N;
    double a[N];
    int o = 0;
    for(int i = 0; i < N ; i++){
        int flag = 0, sum1 = 0;
        string str;
        cin >> str;
        for(int j = 0; j < str.size(); j++){
            int h = 0;
            if(str[0] != '-' && str[j] > '9' && (str[j] < '0' || str[j] != '.'))
               flag = 1;
            if(str[j] == '-'  && j != 0)
               flag = 1;
            if(str[0] == '.' || (str[0] == '-' && str[1] == '.'))
               flag = 1;
            if(str[j] == '.'){
                for(int s = j +1; s < str.size(); s++){
                    if(str[s] == '.')
                       flag = 1;
                    else
                       sum1++;
                }
                if(sum1 > 2)
                   flag = 1;
            }
        }
        if(str.size() == 1 && str[0] == '-')
           flag = 1;
        if(flag == 0)
        {
        if(stod(str) > 1000.0 || stod(str) < -1000.0)
           flag = 1;
        }
        if(flag == 0){
            a[o++] = stod(str);
        }
        if(flag == 1){
            cout << "ERROR: " << str << " is not a legal number" << endl;
        }
    }
    double sum = 0;
    if(o > 0){
        for(int i = 0; i < o ; i++){
            sum += a[i];
        }
        if(o == 1)
        printf("The average of 1 number is %.2lf",sum/o);
        else
        printf("The average of %d numbers is %.2lf",o,sum/o);
    }
    else 
        cout << "The average of 0 numbers is Undefined";
    return 0;

}

Guess you like

Origin www.cnblogs.com/zhulmz/p/12193528.html