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 ;
}

最新文章

  1. 通过innobackupex实现对MySQL的增量备份与还原
  2. 基础学习day10--异常、包
  3. hdu 4738 Caocao&#39;s Bridges 图--桥的判断模板
  4. InfluxDb系列:几个关键概念(主要是和关系数据库做对比)
  5. magento currency magento头部增加币种切换选择
  6. python 错误处理
  7. as。 对象和数组
  8. C# 使用Nlog记录日志到数据库 使用LogEventInfo类获取,命名空间名称、类名、方法名
  9. 文件夹65ad47d7-2e27-4a5c-b238-26643fdaeb98
  10. 使用apache daemon让java程序在unix系统上以服务方式运行
  11. Android 布局(线性布局、相对布局)
  12. 考了3年,工作四年,零基础在职终于拿到CFA证书
  13. java如何输入数据
  14. java并发编程基础 --- 7章节 java中的13个原子操作类
  15. repo
  16. Python学习—数据库篇之SQL补充
  17. ios开发之--给WebView加载进度条
  18. 手机端touch事件封装
  19. git五分钟教程
  20. iOS Swift WisdomKeyboardKing 键盘智能管家SDK

热门文章

  1. vue-cli 上传图片上传到OSS(阿里云)
  2. git format-patch制作内核补丁
  3. Java多线程之线程状态转换图
  4. Visual Studio常用插件整理
  5. 红米Note 7 Pro在印度首销迅速售罄
  6. kubernetes Helm基本操作
  7. CDQ题目套路总结 [未完成]
  8. Hibernate 注解映射
  9. Go Deeper HDU - 3715(2 - sat 水题 妈的 智障)
  10. 【XSY2774】学习 带花树