1108 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 <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e3; int n, cnt = ;
double ans = 0.0;
char s[MAX], notlegal[][MAX]; bool my_is_ans()
{
int len = strlen(s), my_cnt = ;
// if (s[0] == '.' || s[len - 1] == '.') return false;
for (int i = ; i < len; ++ i)
{
if (isalpha(s[i])) return false;
if (s[i] == '.')
{
my_cnt ++;
if (my_cnt >= ) return false;
if (i <= len - ) return false;
}
}
return true;
} int main()
{
// freopen("Date1.txt", "r", stdin);
scanf("%d", &n);
for (int i = ; i <= n; ++ i)
{
scanf("%s", &s);
double d; if (!my_is_ans() || sscanf(s, "%lf", &d)!= || d < - || d > )
{
strcpy(notlegal[cnt ++], s);
continue;
}
ans += d;
} for (int i = ; i < cnt; ++ i)
printf("ERROR: %s is not a legal number\n", notlegal[i]);
if (n - cnt == )
printf("The average of 0 numbers is Undefined\n");
else if (n - cnt == )
printf("The average of 1 number is %.2lf\n", ans);
else
printf("The average of %d numbers is %.2lf\n", n - cnt, ans / (n - cnt));
return ;
}

pat 1108 Finding Average(20 分)的更多相关文章

  1. PAT甲级——1108.Finding Average (20分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  2. PAT Advanced 1108 Finding Average (20 分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  3. 【PAT甲级】1108 Finding Average (20分)

    题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...

  4. Day 007:PAT训练--1108 Finding Average (20 分)

    话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...

  5. PAT 1108 Finding Average [难]

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  6. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  7. PAT甲题题解-1108. Finding Average (20)-字符串处理

    求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...

  8. PAT 1108 Finding Average

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  9. 1108 Finding Average (20 分)

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

随机推荐

  1. opencv::像素重映射

    像素重映射(cv::remap) 简单点说就是把输入图像中各个像素按照一定的规则映射到另外一张图像的对应位置上去,形成一张新的图像. Remap( InputArray src, // 输入图像 Ou ...

  2. 2019.10.24 CSP%你赛第二场d1t3

    题目描述 Description 精灵心目中亘古永恒的能量核心崩溃的那一刻,Bzeroth 大陆的每个精灵都明白,他们的家园已经到了最后的时刻.就在这危难关头,诸神天降神谕,传下最终兵器——潘少拉魔盒 ...

  3. OSI七层模型和五层TCP/IP协议

    1.查公网ip的方法: windows,打开浏览器,访问百度,搜IP即可 linux:curl ifconfig.me 2.OSI七层模型 ==网络工程师:== 物理层 1层,通信介质的信号到数字信号 ...

  4. maven 打包 spring boot 生成docker 镜像

    1.所使用材料 ,spring boot 项目 基于maven ,maven 工具, docker工具 ps:为啥使用 docker 公司微服务需要启动太多,有两个优点吧! 1.方便管理,2.减少服务 ...

  5. 【RabbitMQ 实战指南】一 延迟队列

    1.什么是延迟队列 延迟队列中存储延迟消息,延迟消息是指当消息被发送到队列中不会立即消费,而是等待一段时间后再消费该消息. 延迟队列很多应用场景,一个典型的应用场景是订单未支付超时取消,用户下单之后3 ...

  6. git 的一些指令

    作为一个前端,git的各种指令应该是需要掌握的,因为前一段时间遇到了一些问题,总觉得每次遇到点问题都去百度这样总归不大好,还是记录一下比较常见的一些指令吧.以后遇到新的再继续添加. 1. 新建本地分支 ...

  7. Spring Boot 配置 - 配置信息加密

    ▶ Spring Boot 依赖与配置 Maven 依赖 <dependency> <groupId>org.springframework.boot</groupId& ...

  8. Text 尺寸获取

    获取text在当前文本内容下应该尺寸: 宽度:text.preferredWidth 高度:text.preferredHeight

  9. 关于kaggle注册无法显示人机验证码问题

    最近准备做项目,需要在kaggle上下载数据集,但注册时遇到了无法显示验证图片信息的问题,我也是通过百度最终找到解决方法,所以就准备记录下来啦:下面是解决步骤: step1:下载Google访问助手 ...

  10. C# 8 - 其它新特性

    其它关于C# 8和.NET Core 3.0新特性的文章: C# 8 - Nullable Reference Types 可空引用类型 C# 8 - 模式匹配 C# 8 - Range 和 Inde ...