美文网首页
1054 求平均值

1054 求平均值

作者: 初见还是重逢 | 来源:发表于2019-04-03 10:34 被阅读0次

本题的基本要求非常简单:给定 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,以及小数点的位置,最后对小数点后面的数字的个数进行判断。

bool judge(string a)
{
    int point = 0, pos = -1, sub = 0;
    //先判断第一个字符是否是数字与负号
    if (!(a[0] == '-' || (a[0] >= '0'&&a[0] <= '9'))) return false;
    for (unsigned int i = 1; i < a.length(); i++)//判断后面是否全是数字或者只有一个小数点
    {
        if (a[i] >= '0'&&a[i] <= '9')
        {
            continue;
        }
        else if (point == 0 && a[i] == '.')
        {
            point++;
            pos = i;//记录小数点位置
            continue;
        }
        else
            return false;
    }
    //判断有没有超出两位小数
    unsigned length = a.length() - pos - 1;//计算小数点到最后一位的长度
    if (pos!=-1&&length>2)return false;
    else return true;
}

判断通过的字符串转化为double类型后,再对其是否在[-1000,1000]区间进行判断,都判断通过的进行加和,不通过的输出错误。

    stringstream ss;
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        cin >> store;
        if (judge(store))//先判断是否是合法数字
        {
            ss << store;
            ss >> temp;
            ss.clear();
            if (temp >= -1000 && temp <= 1000)//如果是合法数字,将其转化为double后判断是否在[-1000,1000]的范围内
            {
                avg += temp;
                legal++;
            }
            else
            {
                cout << "ERROR: " << store << " is not a legal number" << endl;
            }
        }
        else
        {
            cout << "ERROR: " << store << " is not a legal number" << endl;
        }
    }

最后输出结果,注意当K=1,numbers应该改为number!

    avg /= legal;
    if (legal == 0)
    {
        cout << "The average of 0 numbers is Undefined" << endl;
    }
    else if (legal==1)//注意k=1的时候,number没有s!
    {
        cout << "The average of " << legal << " number is ";
        printf("%.2f\n", avg);
    }
    else
    {
        cout << "The average of " << legal << " numbers is ";
        printf("%.2f\n", avg);
    }

代码:

求平均值

//1054 求平均值
#include<iostream>
#include<string>
#include<vector>
#include<sstream>

using namespace std;

bool judge(string a)
{
    int point = 0, pos = -1, sub = 0;
    //先判断第一个字符是否是数字与负号
    if (!(a[0] == '-' || (a[0] >= '0'&&a[0] <= '9'))) return false;
    for (unsigned int i = 1; i < a.length(); i++)//判断后面是否全是数字或者只有一个小数点
    {
        if (a[i] >= '0'&&a[i] <= '9')
        {
            continue;
        }
        else if (point == 0 && a[i] == '.')
        {
            point++;
            pos = i;//记录小数点位置
            continue;
        }
        else
            return false;
    }
    //判断有没有超出两位小数
    unsigned length = a.length() - pos - 1;//计算小数点到最后一位的长度
    if (pos!=-1&&length>2)return false;
    else return true;
}


int main()
{
    int N;
    int legal = 0;
    string store;
    double avg = 0, temp;
    stringstream ss;
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        cin >> store;
        if (judge(store))//先判断是否是合法数字
        {
            ss << store;
            ss >> temp;
            ss.clear();
            if (temp >= -1000 && temp <= 1000)//如果是合法数字,将其转化为double后判断是否在[-1000,1000]的范围内
            {
                avg += temp;
                legal++;
            }
            else
            {
                cout << "ERROR: " << store << " is not a legal number" << endl;
            }
        }
        else
        {
            cout << "ERROR: " << store << " is not a legal number" << endl;
        }
    }
    avg /= legal;
    if (legal == 0)
    {
        cout << "The average of 0 numbers is Undefined" << endl;
    }
    else if (legal==1)//注意k=1的时候,number没有s!
    {
        cout << "The average of " << legal << " number is ";
        printf("%.2f\n", avg);
    }
    else
    {
        cout << "The average of " << legal << " numbers is ";
        printf("%.2f\n", avg);
    }
    return 0;
}

相关文章

网友评论

      本文标题:1054 求平均值

      本文链接:https://www.haomeiwen.com/subject/pmrabqtx.html