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 ...
随机推荐
- Tesnsorflow命名空间与变量管理参数reuse
一.TensorFlow中变量管理reuse参数的使用 1.TensorFlow用于变量管理的函数主要有两个: (1)tf.get_variable:用于创建或获取变量的值 (2)tf.varia ...
- 表格组件---bootstrapTable
bootstrapTable中文官方网站http://bootstrap-table.wenzhixin.net.cn1.文件引用 //1.引用Jquery <script src=" ...
- linux下的进程间通信之共享内存
概念:这种机制允许两个或多个进程通过把公共数据结构放入一个共享内存区来访问它们.如果进程要访问这种数据结构所在的共享内存区,就必须在自己的地址空间中增加一个新线性区,新线性区映射与这个共享内存区相关的 ...
- gd库的相关内容
gd库注意事项 对于乱码问题 在php里面包含 header("content-type:image/png"); 这样输出的图像就不会乱码了后面跟的Png也可以改变为自己想要输出 ...
- CEIWEI CommMonitor 串口监控精灵11.0 SDK/OCX 串口过滤驱动
CommMonitorX 监视精灵SDK,能够嵌入到你的App程序中,从而在你的App中实现串行端口分析.调试串行设备的协议信息,并可以拦截.记录串行端口程序操作串口的TX.Rx数据包.串口置信息如波 ...
- CEIWEI USBMonitor监控驱动 OCX/SDK USB 监控精灵 USB过滤驱动
CEIWEI USBMonitor监控精灵软件SDK USBMonitorX.dll SDK,能够嵌入到你的App程序中,从而在你的App中实现USB端口协议分析.调试USB设备的协议信息,并可以拦截 ...
- WebGL半透明物体的绘制
WebGL 中当透明和半透明物体共存时,相关设置不正确的话,物体表面会出现破碎杂乱的断面,非常影响效果,我们接着就来解决这个问题. 完成的展示Demo请看: 半透明物体和透明物体共存 α 混合 让物体 ...
- 学习笔记:oracle学习三:SQL语言基础之检索数据:简单查询、筛选查询
目录 1. 检索数据 1.1 简单查询 1.1.1 检索所有列 1.1.2 检索指定的列 1.1.3 查询日期列 1.1.4 带有表达式的select语句 1.1.5 为列指定别名 1.1.6 显示不 ...
- 数据结构 -- Trie字典树
简介 字典树:又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高. 性质: 1. 根节 ...
- FishingMaster(HDU-6709)【贪心】
题目链接:https://vjudge.net/problem/HDU-6709 题意:一个人要抓n条鱼,每抓一条鱼用时K,每烹饪一条鱼用时a[i],抓鱼的过程不能被打断,烹饪鱼的时候可以抓鱼,也可以 ...