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 [−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:

  1. 7
  2. 5 -3.2 aaa 9999 2.3.4 7.123 2.35

Sample Output 1:

  1. ERROR: aaa is not a legal number
  2. ERROR: 9999 is not a legal number
  3. ERROR: 2.3.4 is not a legal number
  4. ERROR: 7.123 is not a legal number
  5. The average of 3 numbers is 1.38

Sample Input 2:

  1. 2
  2. aaa -9999

Sample Output 2:

  1. ERROR: aaa is not a legal number
  2. ERROR: -9999 is not a legal number
  3. The average of 0 numbers is Undefined
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <string>
  6. #include <map>
  7. #include <stack>
  8. #include <vector>
  9. #include <queue>
  10. #include <set>
  11. #define LL long long
  12. #define INF 0x3f3f3f3f
  13. using namespace std;
  14. const int MAX = 1e3;
  15.  
  16. int n, cnt = ;
  17. double ans = 0.0;
  18. char s[MAX], notlegal[][MAX];
  19.  
  20. bool my_is_ans()
  21. {
  22. int len = strlen(s), my_cnt = ;
  23. // if (s[0] == '.' || s[len - 1] == '.') return false;
  24. for (int i = ; i < len; ++ i)
  25. {
  26. if (isalpha(s[i])) return false;
  27. if (s[i] == '.')
  28. {
  29. my_cnt ++;
  30. if (my_cnt >= ) return false;
  31. if (i <= len - ) return false;
  32. }
  33. }
  34. return true;
  35. }
  36.  
  37. int main()
  38. {
  39. // freopen("Date1.txt", "r", stdin);
  40. scanf("%d", &n);
  41. for (int i = ; i <= n; ++ i)
  42. {
  43. scanf("%s", &s);
  44. double d;
  45.  
  46. if (!my_is_ans() || sscanf(s, "%lf", &d)!= || d < - || d > )
  47. {
  48. strcpy(notlegal[cnt ++], s);
  49. continue;
  50. }
  51. ans += d;
  52. }
  53.  
  54. for (int i = ; i < cnt; ++ i)
  55. printf("ERROR: %s is not a legal number\n", notlegal[i]);
  56. if (n - cnt == )
  57. printf("The average of 0 numbers is Undefined\n");
  58. else if (n - cnt == )
  59. printf("The average of 1 number is %.2lf\n", ans);
  60. else
  61. printf("The average of %d numbers is %.2lf\n", n - cnt, ans / (n - cnt));
  62. return ;
  63. }

pat 1108 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 Advanced 1108 Finding Average (20 分)

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

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

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

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

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

  5. PAT 1108 Finding Average [难]

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

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

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

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

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

  8. PAT 1108 Finding Average

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

  9. 1108 Finding Average (20 分)

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

随机推荐

  1. [Luogu3878] [TJOI2010]分金币

    题目描述 现在有n枚金币,它们可能会有不同的价值,现在要把它们分成两部分,要求这两部分金币数目之差不超过1,问这样分成的两部分金币的价值之差最小是多少? 输入输出格式 输入格式: 每个输入文件中包含多 ...

  2. [牛客网NOIP赛前集训营-提高组(第一场)]C.保护

    链接:https://www.nowcoder.com/acm/contest/172/C来源:牛客网 题目描述 C国有n个城市,城市间通过一个树形结构形成一个连通图.城市编号为1到n,其中1号城市为 ...

  3. python学习-语言概述(一)

    1.python的特点 python是一种面向对象.解释型.弱类型的脚本语言,它也是一种功能强大而完善的通用型语言. 解释性语言的特点:速度慢:源代码加密困难:跨平台:

  4. HTTP使用get,post方式连接

    在项目中使用了http的get和post方式连接,发送传输数据: public static String doGet(String httpUrl) { HttpURLConnection conn ...

  5. 【教程】基于Ubuntu系统的PyTorch虚拟环境配置

    目录 一.PyTorch虚拟环境配置 二.PyTorch虚拟环境使用 三.常用命令 Editor: Veagau Time: 2019/10/17 一.PyTorch虚拟环境配置 该部分操作均在终端( ...

  6. MOOC C++笔记(七)输入输出流

    输入输出流 与输入输出流操作相关的类 istream:是用于输入的流类,cin就是该类的对象. ostream:是用于输出的流类,cout就是该类的对象. ifstream:是用于从文件读取数据的类. ...

  7. Activity 学习(一) 插件安装篇

    目录 Ider下安装 Eclipse下安装 Ider安装图解 首先,创建一个普通的Java工程即可,然后按照下面流程进行: 1:点击菜单中的File(最左上角),选择settings 2:plugin ...

  8. Spring框架 --- 深入

    1.Spring IOC IOC技术: 控制反转,也叫(依赖注入) 控制反转:Bean的生命周期不受你控制,而是交给Spring容器管理. Spring框架如何利用IOC ?:        实现了控 ...

  9. .net core跨平台应用研究-ubuntu core下配置.net core运行时

    引言 年初研究了一阵子.net core跨平台应用,先后发表了几篇应用研究的文章.因工作原因,忙于项目上线,有一阵子没来博客园写文章了.最近项目基本收尾,抽空翻了下自己的博客,廖廖几篇文章,真让人汗颜 ...

  10. 页面离开前提示用户(onbeforeunload 事件)

    window.onbeforeunload = function (e) { var evt = e || window.event; evt.returnValue = '离开会使编写的内容丢失'; ...