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 <vector>
using namespace std;
#define inf 1000
double sum = 0.0, number;
int n, nums = ;
int main()
{
cin >> n;
char str1[], str2[];
while (n--)
{
cin >> str1;
bool flag = true;
sscanf(str1, "%lf", &number);//将字符转换为浮点数值
sprintf(str2, "%0.2f", number);//按要求输出
for (int i = ; str1[i] != '\0'&& flag; ++i)
if (str1[i] != str2[i])
flag = false;
if (!flag || number<-inf || number>inf)
cout << "ERROR: " << str1 << " is not a legal number" << endl;
else
{
nums++;
sum += number;
}
}
cout << "The average of " << nums << (nums == ? " number is " : " numbers is ");
if (nums == )
cout << "Undefined" << endl;
else
printf("%0.2f\n", sum / nums);
return ;
}

PAT甲级——A1108 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】A1108 Finding Average (20 分)

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

  3. PAT Advanced 1108 Finding Average (20 分)

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

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

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

  5. PAT A1108 Finding Average (20 分)——字符串,字符串转数字

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

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

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

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

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

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

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

  9. PAT 甲级 1035 Password (20 分)

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

随机推荐

  1. ES6点点点运算符

    1. rest(可变)参数 * 用来取代arguments 但比arguments灵活,只能是最后部分形参参数 function add(...values) { let sum = ; for(va ...

  2. zabbix active模式以及自定义key not Supported的解决

    zabbix active模式 active模式适用场景 zabbix server端无法直连agent端,比如agent为内网机器,仅有内网ip,没有公网ip,但是内网机器能够访问server端 a ...

  3. Spring - @ManagedResource, @ManagedOperation, @ManagedAttribute

    总结 通过annotation (@ManagedResource, @ManagedOperation, @ManagedAttribute)注解注册MBean到JMX实现监控java运行状态 参考 ...

  4. luoguP1134 阶乘问题 [数论]

    题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001, ...

  5. NX二次开发-获得图纸抑制尺寸的表达式UF_DRF_ask_controlling_exp

    #include <uf.h> #include <uf_modl.h> #include <uf_drf.h> #include <uf_obj.h> ...

  6. NX二次开发-NXOPEN找相切面方法ScRuleFactory()->CreateRuleFaceTangent

    #include <uf_defs.h> #include <uf_ui_types.h> #include <iostream> #include <NXO ...

  7. [luogu 4389] 付公主的背包

    题意:求一个较大的多重背包对于每个i的方案数,答案对998244353取模. 思路: 生成函数: 对于一个\(V\) 设: \(f(x) = \sum_{i=0}^{oo} x ^ {V * i} = ...

  8. WinDbg解决BHO不加载

    昨天zhengwei同学说他机器上的一个BHO不能正常加载,我把BHO的代码拿过来,在我的两台机器上都验证了一下,一台是Win7+IE8的环境,一台是XP+IE7的环境,都能正常加载.zhengwei ...

  9. jQuery 快捷操作

    快捷操作 1. class属性值操作 $().attr(‘class’,值); $().attr(‘class’); $().removeAttr(‘class’);  //删除class的所有属性 ...

  10. UVA 10382 Watering Grass 贪心+区间覆盖问题

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...