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. 【VS开发】【数据库开发】windows下libevent x64库静态编译

    按照libevent的文档,使用VC的nmake -f Makefile.nmake即可编译32位release模式.因为项目中要求编译64位的版本,需要在Makefile.nmake中添加一个LIB ...

  2. 日常工作问题解决:配置NTP服务器以及一些常见错误解决

    1.配置NTP服务端 环境:redhat 6.5 服务器主机名 ip地址 说明 server 192.168.57.20 NTP服务端 client 192.168.57.21 NTP客户端 搭建说明 ...

  3. Mstering QT5 chapter1

    涉及到c++ 14新特性: lambda,autovariables. A basic .pro file generally contains: 1) Qt modules used (core, ...

  4. Win7 JavaEE 安装

    新建四个目录 D:\ApacheServer\eclipse 存放eclipse D:\ApacheServer\jdk jdk安装目录 D:\ApacheServer\apache-tomcat 存 ...

  5. 《Mysql - 为什么表数据删掉一半,表文件大小不变?》

    一:概念 - 这里,我们还是针对 MySQL 中应用最广泛的 InnoDB 引擎展开讨论. - 一个 InnoDB 表包含两部分,即:表结构定义和数据. - 在 MySQL 8.0 版本以前,表结构是 ...

  6. PO,VO,DAO,BO,POJO之间的区别与解释

    VO value object:值对象 通常用于业务层之间的数据传递,由new创建,由GC回收. PO persistant object:持久层对象 对应数据库中表的字段. VO和PO,都是属性加上 ...

  7. 使用Laravel 和 Vue 构建一个简单的SPA

    本教程是作者自己在学习Laravel和Vue时的一些总结,有问题欢迎指正. Laravel是PHP的一个框架,Vue是前端页面的框架,这两个框架如何结合起来构建一个SPA(Single Page Ap ...

  8. (九)springmvc之json的处理(服务端发送json数据到客户端)

    一.json处理方法有两种 1:导入Spring需要json的jar包.(本例使用) 使用@ResponseBody该注解用于将Controller的方法返回的对象,通过HttpMessageConv ...

  9. IdentityServer3 使用记录

    官方教程:https://identityserver.github.io/Documentation/docsv2/overview/mvcGettingStarted.html 1.是否启用 SS ...

  10. core项目打包时发现有的项目的xml文件不会被打包进去,怎么办?

    我打包后发现打包后的文件夹内,不存在xml文件,所以swagger加载失败:然后经过测试发现Core项目打包的时候是默认不包含Xml文件的,VS里面也没有办法设置. 解决方法:手动修改项目文件,找到你 ...