PTA Finding Average(20分)

無限の光を放つのは人間の心であり、無限の闇を生み出すのも人間の心であり、光と闇が絡み合って戦うこの世界は、懐かしくて無力な世界です。

基本的なタスクは単純です。N個の実数が与えられると、それらの平均を計算することになっています。しかし、複雑になるのは、入力された数値の一部が正当でない可能性があることです。 法律上の  入力が[-1000,1000]で実数であるとより多くない小数点以下2桁の精度アップです。平均を計算するとき、それらの不正な数値はカウントされません。

入力仕様:

各入力ファイルには1つのテストケースが含まれています。いずれの場合も、最初の行は正の整数N(≤100)を示します。次に、1つのスペースで区切られた次の行にN個の数値が表示されます。

出力仕様:

不正な入力番号ごとに 、が入力である行  ERROR: X is not a legal number に  X出力します。次に、最後に結果を1行に出力します。  The average of K numbers is Y ここ  K で、は有効な入力の数で  Y あり、それらの平均で、小数点以下2桁まで正確です。平均を計算できない場合は、のUndefined 代わりに  出力します  YK が1の場合  は、The average of 1 number is Y 代わりに出力さ  れます。

入力例1:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

出力例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

入力例2:

2
aaa -9999

出力例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