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 [-1000, 1000] 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 (<=100). 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<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
char num[];
const double INF = 100000000.0;
double prase_(char num[]){
int len = strlen(num);
int pos = -, cnt = ;
int tag = ;
for(int i = ; num[i] != '\0'; i++){
if(!(num[i] >= '' && num[i] <= '' || num[i] == '.' || num[i] == '-')){
return INF;
}
if(num[i] == '.')
cnt++;
if(cnt > )
return INF;
}
if(num[] == '-')
tag = ;
for(pos = ; num[pos] != '.' && num[pos] != '\0'; pos++);
int P = , ansL = , ansR = ;
if(pos == '\0'){
for(int i = len - ; i >= tag; i--){
ansL += (num[i] - '') * P;
P *= ;
}
if(tag == )
return ansL * -1.0;
else return ansL * 1.0;
}else{
if(len - pos - > )
return INF;
P = ; ansL = ;
for(int i = pos - ; i >= tag; i--){
ansL += (num[i] - '') * P;
P *= ;
}
P = ; ansR = ;
for(int i = len - ; i > pos; i--){
ansR += (num[i] - '') * P;
P *= ;
}
double temp = ansR * 1.0;
for(int i = ; i < len - pos - ; i++){
temp *= 0.1;
}
if(tag == )
return ((double)ansL + temp) * -1.0;
else return (double)ansL + temp;
}
}
int main(){
int N, cnt = ;
double sum = , temp = ;
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%s", num);
temp = prase_(num);
if(temp > 1000.0 || temp < -1000.0){
printf("ERROR: %s is not a legal number\n", num);
}else{
sum += temp;
cnt++;
}
}
if(cnt > ){
double prt = sum / (double)cnt;
printf("The average of %d numbers is %.2f\n", cnt, prt);
}else if (cnt == ){
printf("The average of 0 numbers is Undefined\n");
}else if(cnt == ){
double prt = sum / (double)cnt;
printf("The average of %d number is %.2f\n", cnt, prt);
}
cin >> N;
return ;
}

A1108. Finding Average的更多相关文章

  1. 【刷题-PAT】A1108 Finding Average (20 分)

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

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

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

  3. PAT甲级——A1108 Finding Average【20】

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

  4. 1108 Finding Average (20 分)

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

  5. Pat1108: Finding Average

    1108. Finding Average (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The b ...

  6. PAT 1108 Finding Average [难]

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

  7. PAT_A1108#Finding Average

    Source: PAT A 1108 Finding Average (20 分) Description: The basic task is simple: given N real number ...

  8. pat 1108 Finding Average(20 分)

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

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

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

随机推荐

  1. fatal: HttpRequestException encountered解决方法

    最近在windows下git push提交就会弹出如下错误: 网上查了一下发现是Github 禁用了TLS v1.0 and v1.1,必须更新Windows的git凭证管理器,才行. https:/ ...

  2. RabbitMQ基本操作

    更加详细的 链接https://www.cnblogs.com/dwlsxj/p/RabbitMQ.html RabbitMQ基础知识 一.背景 RabbitMQ是一个由erlang开发的AMQP(A ...

  3. awk骚操作

    一.awk自加 [root@168web3 ~]# head /data/logs/cloud_monitor_rds_cpu.log |awk '{sum+=$NF}END{print sum}' ...

  4. 重写TreeView模板来实现数据分层展示(二)

    前面一片文章实现TreeView的基本的模板重写,那么照着这个思路,我们再来写一个稍稍复杂的TreeView ,其它的内容都和前面系列内容相似,还是和之前文章介绍的一样,首先看看做出的DEMO的最终样 ...

  5. npm 设置代理

    设置代理 npm config set proxy http://username:password@server:portnpm config set https-proxy http://user ...

  6. Dreamweaver怎样用Edge Web Fonts功能

    https://jingyan.baidu.com/article/37bce2beb3af6f1002f3a2c9.html

  7. SpringBoot之通过yaml绑定注入数据

    依赖包: <!--配置文件注解提示包--> <dependency> <groupId>org.springframework.boot</groupId&g ...

  8. scrapy 项目搭建

    安装好scrapy后,开始创建项目 项目名:zhaopin   爬虫文件名:zhao 1:cmd  --  scrapy startproject zhaopin 2:cd zhaopin,进入项目目 ...

  9. JavaScript 使用 toJSON 方法格式化日期

    toJSON 方法可以将 Date 对象转换为 ISO-8601 标准的字符串:YYYY-MM-DDTHH:mm:ss. sssZ var date = new Date(); // toJSON() ...

  10. 打包一个UWP APP

    Before packaging your app Test your app. Before you package your app for store submission, make sure ...