1054 求平均值 (20 分)测试点2、3

1054 求平均值 (20 分)

本题的基本要求非常简单:给定 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

代码如下

#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<string.h>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<iostream>

using namespace std;




int main()
{
    int n;
    scanf("%d", &n);
    int num = 0;
    double count = 0;
    string str;
    for (int i = 0; i < n; i++)
    {
        cin >> str;
        bool fu = false;
        double temp = 0;
        bool point = false;
        int j = 1;
        bool pr = false;
        if (str[0] == '-')
        {
            fu = true;
        }
        else if (str[0] >= '0' && str[0] <= '9')
        {
            temp = temp * 10 + str[0] - '0';
        }
        else
        {
            printf("ERROR: %s is not a legal number\n", str.c_str());
            continue;
        }
        for ( j = 1; j < str.size(); j++)//小数点前
        {
            if (str[j] <= '9' && str[j] >= '0')
            {
                 temp = temp * 10 + str[j] - '0';
            }
            else if (str[j] == '.')
            {
                point = true;
                break;
            }
            else
            {
                printf("ERROR: %s is not a legal number\n", str.c_str());
                pr = true;
                break;
            }
        }
        if (pr == true)continue;
        if (point == true)
        {
            if (str.size() - j  > 3)//超两位
            {
                printf("ERROR: %s is not a legal number\n", str.c_str());
                continue;
            }
            else if (str.size()-1 == j)
            {

            }
            else
            {
                if (str[j + 1] >= '0' && str[j + 1] <= '9' && str[j + 2] >= '0' && str[j + 2] <= '9')
                {
                    temp = temp + (str[j + 1] - '0') * 0.1 + (str[j + 2] - '0') * 0.01;
                }
                else if (str[j + 1] >= '0' && str[j + 1] <= '9')
                {
                    temp = temp + (str[j + 1] - '0') * 0.1;
                }
                else
                {
                    printf("ERROR: %s is not a legal number\n", str.c_str());
                    continue;
                }
            }
        }
        if (fu)
        {
            temp = -temp;
        }
        if (temp < -1000 || temp>1000)
        {
            printf("ERROR: %s is not a legal number\n", str.c_str());
            continue;
        }
        count += temp;
        num++;
    }
    if (num == 0)
    {
        printf("The average of 0 numbers is Undefined");
    }
    else if (num == 1)
    {
        printf("The average of %d number is %.2lf", num, count / num);
    }
    else
    {
        printf("The average of %d numbers is %.2lf", num, count / num);
    }
    return 0;
}

总结

题目没提到的如“526.”的输入也是要被视为是正确的输入,还有就是注意末尾如果 K 为 1,则输出 The average of 1 number is Y。,是number而不是numbers

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 滑动平均值滤波是一种常用的数字信号处理方法,在Python中可以通过以下代码实现。 ```python def sliding_average_filter(data, window_size): filtered_data = [] window = [] for i, value in enumerate(data): window.append(value) if len(window) > window_size: window.pop(0) filtered_value = sum(window) / len(window) filtered_data.append(filtered_value) return filtered_data ``` 以上代码实现了一个滑动平均值滤波的函数,它接受两个参数:`data`是原始输入数据的列表,`window_size`是窗口大小,即计算平均值的数据点数量。 在函数中,我们首先定义了一个空的窗口列表`window`和一个用于存储过滤后数据的列表`filtered_data`。然后,我们遍历输入数据列表`data`,并依次将每个数据点添加到窗口中。如果窗口的长度超过了指定的窗口大小,我们就从窗口的开头移除最早加入的数据点。 在每个数据点被添加到窗口后,我们通过窗口中所有数据点的和除以窗口大小,得到当前的滑动平均值,并将其添加到过滤后数据的列表`filtered_data`中。 最后,函数返回过滤后的数据列表`filtered_data`。 使用示例代码如下: ```python data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] window_size = 3 filtered_data = sliding_average_filter(data, window_size) print(filtered_data) ``` 以上示例代码输入了一个数据列表`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`和窗口大小为3,然后调用了滑动平均值滤波函数`sliding_average_filter`进行滤波处理,最后输出滤波后的数据列表。 滑动平均值滤波可以有效平滑输入数据的变化,提取出其趋势或周期性。在实际应用中,可以根据需要调整窗口大小以达到更好的滤波效果。 ### 回答2: Python滑动平均值滤波即通过对一组数据进行移动窗口的平均值计算,来消除数据中的噪声。滑动平均值滤波常用于信号处理和数据分析等领域。 在Python中,可以通过以下步骤实现滑动平均值滤波: 1. 定义窗口大小:根据需要平滑的数据长度,确定一个窗口的大小。窗口大小决定了平滑效果的精细程度,一般选择一个适当的窗口大小(如3、5、10等)。 2. 定义一个列表或数组:用于存储待平滑的数据。假设原始数据存储在一个名为data的列表中。 3. 初始化滑动窗口:根据窗口大小,从原始数据列表中截取相应长度的子列表作为初始滑动窗口。 4. 计算初始平均值:对初始滑动窗口中的数据进行和,并除以窗口大小,得到初始平均值。 5. 滑动窗口移动:从原始数据列表中按顺序取出下一个数据,将滑动窗口向右移动一个位置,即去掉窗口中最左边的数据,加入窗口中最右边的新数据。 6. 计算滑动平均值:对新的滑动窗口中的数据进行和,并除以窗口大小,得到滑动平均值。 7. 将当前滑动平均值存储到一个新的列表或数组中,以便后续使用。 8. 重复步骤5至7,直到遍历完原始数据列表,得到所有数据的滑动平均值。 最后,可以根据需要对滑动平均值进行进一步的处理,如绘制图表、计算其他统计指标等。 需要注意的是,滑动平均值滤波对于突变或快速变化的信号可能会引入较大的延迟,因此在应用滑动平均值滤波时需要根据具体情况进行调整。 ### 回答3: 滑动平均值滤波是一种信号处理技术,可以平滑原始信号、减少噪音并提取出信号的趋势。在Python中,我们可以使用以下代码实现滑动平均值滤波。 首先,创建一个空的列表,用于存储原始信号序列和平滑后的信号序列。然后,定义一个滑动窗口的大小,该窗口的大小决定了平均值滤波的效果。接下来,使用一个循环遍历原始信号序列,并计算滑动窗口内数据的平均值。将计算得到的平均值添加到平滑后的信号序列中。 下面是滑动平均值滤波的Python代码示例: ```python def moving_average_filter(data, window_size): averaged_data = [] for i in range(len(data)): if i < window_size: averaged_data.append(sum(data[:i+1])/(i+1)) else: averaged_data.append(sum(data[i-window_size+1:i+1])/window_size) return averaged_data # 测试示例 original_data = [3, 5, 7, 2, 8, 10, 11, 65, 72, 81, 99, 100, 150] window_size = 3 smoothed_data = moving_average_filter(original_data, window_size) print(smoothed_data) ``` 在这个示例中,我们使用了一个包含13个元素的原始信号序列和窗口大小为3的滑动窗口。运行代码后,将打印出平滑后的信号序列: ```python [3.0, 4.0, 5.0, 4.666666666666667, 5.666666666666667, 6.666666666666667, 9.666666666666666, 28.666666666666668, 49.333333333333336, 72.66666666666667, 93.33333333333333, 97.0, 116.33333333333333] ``` 这个序列是经过滑动平均值滤波处理后的结果,可以看到噪音被平滑了,并且整体信号的趋势也更加明显。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值