PAT Advanced 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 [−] 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 分)的更多相关文章
- PAT甲级——1108.Finding Average (20分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- Day 007:PAT训练--1108 Finding Average (20 分)
话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...
- 【PAT甲级】1108 Finding Average (20分)
题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...
- PAT (Advanced Level) 1108. Finding Average (20)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1108. Finding Average (20)-字符串处理
求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...
- 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 ...
- 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 ...
- 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 ...
- PAT Advanced 1073 Scientific Notation (20 分)
Scientific notation is the way that scientists easily handle very large numbers or very small number ...
随机推荐
- .net视频截图功能,没测试
/// <summary> /// @从视频文件截图,生成在视频文件所在文件夹 /// 在Web.Config 中需要两个前置配置项: /// 1.ffmpeg.exe文件的路径 /// ...
- 修改iPhone5s crash 问题
- CEIWEI CheckSum CRC校验精灵v2.1 CRC3/CRC4/CRC5/CRC6/CRC8CRC10/CRC11/CRC16/CRC24/CRC32/CRC40/CRC64/CRC82/Adler32
CEIWEI CheckSum CRC校验精灵 是一款通用的循环冗余校验码CRC(Cyclic Redundancy Check).MD5.SHA1.SHA2.SHA3.HAVAL.SHAKE.TIG ...
- 关于cookies、sessionStorage和localStorage解释及区别
在浏览器查看 HTML4的本地存储 cookie 浏览器的缓存机制提供了可以将用户数据存储在客户端上的方式,可以利用cookie,session等跟服务端进行数据交互. 一.cookie和sessio ...
- solr新建core
- python+unittest框架第三天unittest之分离测试固件和公共代码,跳过案例的执行
我们在时间工作中,会将整个项目的代码分别放置多个模块中去编写.方便后期项目维护,比如,我们的web项目可能有多个IP地址,每个IP地址代表不同的测试环境.测试环境与Bat环境或者验收环境等.这就需要我 ...
- linux中查看磁盘容量的常用操作
linux中查看磁盘容量常用操作 实验室有GPU集群,用户跑数据时候跑着跑着会出现集群挂掉的问题,原因就是,在跑数据时,用户上传文件,数据集,系统产生缓存等一系列操作,消耗了集群空间,师兄让我清理下服 ...
- windows下图形学视觉基本库安装不完全指南
安装各种库东奔西走...... GLUT(英文全写:OpenGL Utility Toolkit)是一个处理OpenGL程式的工具库,负责处理和底层操作系统的呼叫以及I/O,并包括了以下常见的功能: ...
- [知乎]鲲鹏920对比intel8180
作者:韩朴宇链接:https://www.zhihu.com/question/308298687/answer/568737742来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- [转帖]Greenplum :基于 PostgreSQL 的分布式数据库内核揭秘 (上篇)
Greenplum :基于 PostgreSQL 的分布式数据库内核揭秘 (上篇) https://www.infoq.cn/article/3IJ7L8HVR2MXhqaqI2RA 学长的文章.. ...