PTA 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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=1010;
int n,ct=0;
double sum=0;
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    cin>>n;
    for(int i=1;i<=n;++i){
        string s;
        cin>>s;
        int fg=0,dd=-1;
        double x=0;
        if(s[0]=='-')
            fg=1;
        for(int i=fg;i<s.size();++i){
            if(s[i]=='.'){
                dd=i;
                break;
            }
            if(s[i]<'0'||s[i]>'9'){
                fg=2;
                break;
            }
            x*=10;
            x+=s[i]-'0';
        }
        if(dd!=-1)
            for(int i=dd+1;i<s.size();++i){
                if(s[i]<'0'||s[i]>'9'){
                    fg=2;
                    break;
                }
                x+=(s[i]-'0')/(1.0*pow(10,(i-dd)));
            }
        if(fg)
            x*=-1;
        if(s.size()-1-dd>2&&dd!=-1)
            fg=2;
        if(fg==0&&dd==0||fg==1&&dd==1)
            fg=2;
        if(x>1000||x<-1000)
            fg=2;
        if(fg==2){
            cout<<"ERROR: "<<s<<" is not a legal number\n";
            continue;
        }
        ct++;
        sum+=x;
    }
    sum/=1.0*ct;
    cout<<"The average of "<<ct;
    if(ct!=1)
        cout<<" numbers is ";
    else
        cout<<" number is ";
    if(!ct)
        cout<<"Undefined";
    else
        printf("%.2f",sum);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44170305/article/details/108511611