PAT 乙级 1054

题目

本题的基本要求非常简单:给定 N 个实数,计算它们的平均值。但复杂的是有些输入数据可能是非法的。一个“合法”的输入是 [−1000,1000] 区间内的实数,并且最多精确到小数点后 2 位。当你计算平均值的时候,不能把那些非法的数据算在内。
输入格式
输入第一行给出正整数 N(≤100)。随后一行给出 N 个实数,数字间以一个空格分隔。
输出格式
对每个非法输入,在一行中输出 ERROR: X is not a legal number,其中 X 是输入。最后在一行中输出结果:The average of K numbers is Y,其中 K 是合法输入的个数,Y 是它们的平均值,精确到小数点后 2 位。如果平均值无法计算,则用 Undefined 替换 Y。如果 K 为 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

思路

  1. 先将题目中的对程序的要求提炼出来
  2. 然后逐条实现
  3. 将负小数的字符串转化为数值的函数方法也很重要

AC代码

/*
1. k == 0 undefined
2. illegal
1) x > 1000 || x < -1000 *
2) 小数点后最多两位 *
3) 字母 *
4) 多个小数点 *
*/

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

vector<double> v;

// 乘0.01是小数点向左移, 不是从左边往右移
double change (string s, int k)
{
    double res = 0; int i = 0;

    double f = 1;
    if(s[0] == '-') f = -f, i += 1;

    for( ; i < s.size() ; i ++ )
    {
        if(s[i] == '.') continue;

        double a = s[i] - '0';
        res = res * 10 + a;
    }

    double floats = pow(0.1, k);

    return res * f * floats;
}

int main ()
{
    int n;
    cin >> n;

    for(int i = 0 ; i < n ; i ++ )
    {
        string s;
        cin >> s;

        int flag = 1, mask = 0, cnt = 0;
        for(int i = 0 ; i < s.size() ; i ++ )
        {
            if(s[i] < '0' || s[i] > '9')  // 非数字
            {
                if(s[i] != '-' && s[i] != '.')
                {
                    flag = 0;
                    break;
                }
                else if(s[i] == '.')
                {
                    if(!mask)  mask = 1;
                    else
                    {
                        flag = 0;
                        break;
                    }
                }
            }
            else
            {
                if(mask)
                {
                    cnt ++ ;
                    if(cnt > 2)
                    {
                        flag = 0;
                        break;
                    }
                }
            }
        }
        if(!flag)
        {
            cout << "ERROR: " << s << " is not a legal number" << endl;
            continue;
        }

        double s_num = change(s, cnt);
        if(s_num > 1000 || s_num < -1000)
        {
            cout << "ERROR: " << s << " is not a legal number" << endl;
            continue;
        }

        v.push_back(s_num);
    }

    if(v.size() == 0) {puts("The average of 0 numbers is Undefined"); return 0;}
    else if(v.size() == 1) {printf("The average of 1 number is %.2lf", v[0]); return 0;}  // 没有s

    double sum = 0;
    for(int i = 0 ; i < v.size() ; i ++ ) sum += v[i];

    int k = v.size();
    double average = sum / k;

    printf("The average of %d numbers is %.2lf", k, average);

    return 0;
}
发布了52 篇原创文章 · 获赞 22 · 访问量 1717

猜你喜欢

转载自blog.csdn.net/qq_45792080/article/details/105120505