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 [−] 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 (≤). 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 <cstring>
using namespace std;
int main()
{
int N;cin>>N;
double avg=;
int val,coun=;
double a;
while(N--){
bool right=true;
char tmp[],tmp2[];
scanf("%s",tmp);
sscanf(tmp,"%lf",&a);
sprintf(tmp2,"%.2f",a);
for(int i=;i<strlen(tmp);i++)
if(tmp[i]!=tmp2[i]) right=false;
if(right&&a<=&&a>=-) {
avg+=a;
coun++;
}
else printf("ERROR: %s is not a legal number\n",tmp);
}
if(coun!=) {
if(coun>){
avg/=coun;
printf("The average of %d numbers is %.2f",coun,avg);
}else printf("The average of 1 number is %.2f",avg);
}else printf("The average of 0 numbers is Undefined"); system("pause");
return ;
}

PAT Advanced 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. Day 007:PAT训练--1108 Finding Average (20 分)

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

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

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

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

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

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

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

  6. PAT Advanced 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  7. PAT Advanced 1042 Shuffling Machine (20 分)(知识点:利用sstream进行转换int和string)

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  8. PAT Advanced 1140 Look-and-say Sequence (20 分)

    Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...

  9. PAT Advanced 1073 Scientific Notation (20 分)

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

随机推荐

  1. iOS-UINavigationController多控制器管理

    UINavigationController 7.8.1 添加子控制器进栈 UINavigationController *nav = [[UINavigationController alloc]  ...

  2. charles 开始/暂停记录

    本文参考:charles 开始/暂停记录 1.1. stop/start recording 和 2.1 recording settings 是常用的功能了:这里需要注意就是后面的session1代 ...

  3. MySQL安装时出现的问题

    mysql正常安装结束之后需要连接你所安装的数据库的时候出现下面的错误: Client does not support authentication protocol requested by se ...

  4. mysql常用操作及常见问题

    常用操作 mysql备份: --整库备份 docker exec 容器ID mysqldump -uroot -p密码 --databases 库名 > 库名.sql --仅导出表和数据 mys ...

  5. 分布式架构下,session共享有什么方案么?

    分布式架构下,session共享有什么方案么? 会点代码的大叔 科技领域创作者 分布式架构下的session共享,也可以称作分布式session一致性:关于这个问题,和大家说一说解决方案(如果有其他的 ...

  6. 【Docker】:使用docker安装redis,挂载外部配置和数据

    普通安装 1.拉取镜像,redis:4.0 docker pull redis:4.0 2.创建redis容器名"redistest1",并开启持久化 docker run -d ...

  7. PAT甲级 进制转换题_C++题解

    进制转换题 PAT (Advanced Level) Practice 进制转换题 目录 <算法笔记> 重点摘要 1015 Reversible Primes (20) 1019 Gene ...

  8. 判断pdf文件是否正常可用

    之前在下载港交所的文件的时候会碰到有些文件异常的情况,文件下载下来,但是不能打开.查到其他的方法不是判断错误就是很麻烦.   整理出一个非常简单的方法,将文件以二进制方式打开,然后判断文件的开头是否符 ...

  9. 去除空格函数trim

    实际查询中,经常存在多个tables,需要统一查询比如segments总大小或者索引或者主键等,我们得到大量的tables表名称,但是SQL查询,每次需要手工添加双引号,去除空格很麻烦. 可以通过文本 ...

  10. hdu 1875 最小生成树 prime版

    最小生成树prime版 大致的步骤 首先选取一个到集合最近的点 然后标记起在集合内部 然后更新最短距离 畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    ...