pat 1108 Finding Average(20 分)
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:
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 <algorithm>
- #include <cstdio>
- #include <cstring>
- #include <string>
- #include <map>
- #include <stack>
- #include <vector>
- #include <queue>
- #include <set>
- #define LL long long
- #define INF 0x3f3f3f3f
- using namespace std;
- const int MAX = 1e3;
- int n, cnt = ;
- double ans = 0.0;
- char s[MAX], notlegal[][MAX];
- bool my_is_ans()
- {
- int len = strlen(s), my_cnt = ;
- // if (s[0] == '.' || s[len - 1] == '.') return false;
- for (int i = ; i < len; ++ i)
- {
- if (isalpha(s[i])) return false;
- if (s[i] == '.')
- {
- my_cnt ++;
- if (my_cnt >= ) return false;
- if (i <= len - ) return false;
- }
- }
- return true;
- }
- int main()
- {
- // freopen("Date1.txt", "r", stdin);
- scanf("%d", &n);
- for (int i = ; i <= n; ++ i)
- {
- scanf("%s", &s);
- double d;
- if (!my_is_ans() || sscanf(s, "%lf", &d)!= || d < - || d > )
- {
- strcpy(notlegal[cnt ++], s);
- continue;
- }
- ans += d;
- }
- for (int i = ; i < cnt; ++ i)
- printf("ERROR: %s is not a legal number\n", notlegal[i]);
- if (n - cnt == )
- printf("The average of 0 numbers is Undefined\n");
- else if (n - cnt == )
- printf("The average of 1 number is %.2lf\n", ans);
- else
- printf("The average of %d numbers is %.2lf\n", n - cnt, ans / (n - cnt));
- return ;
- }
pat 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 ...
- PAT Advanced 1108 Finding Average (20 分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- 【PAT甲级】1108 Finding Average (20分)
题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...
- Day 007:PAT训练--1108 Finding Average (20 分)
话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...
- PAT 1108 Finding Average [难]
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- 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 1108 Finding Average
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- 1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
随机推荐
- [Luogu3878] [TJOI2010]分金币
题目描述 现在有n枚金币,它们可能会有不同的价值,现在要把它们分成两部分,要求这两部分金币数目之差不超过1,问这样分成的两部分金币的价值之差最小是多少? 输入输出格式 输入格式: 每个输入文件中包含多 ...
- [牛客网NOIP赛前集训营-提高组(第一场)]C.保护
链接:https://www.nowcoder.com/acm/contest/172/C来源:牛客网 题目描述 C国有n个城市,城市间通过一个树形结构形成一个连通图.城市编号为1到n,其中1号城市为 ...
- python学习-语言概述(一)
1.python的特点 python是一种面向对象.解释型.弱类型的脚本语言,它也是一种功能强大而完善的通用型语言. 解释性语言的特点:速度慢:源代码加密困难:跨平台:
- HTTP使用get,post方式连接
在项目中使用了http的get和post方式连接,发送传输数据: public static String doGet(String httpUrl) { HttpURLConnection conn ...
- 【教程】基于Ubuntu系统的PyTorch虚拟环境配置
目录 一.PyTorch虚拟环境配置 二.PyTorch虚拟环境使用 三.常用命令 Editor: Veagau Time: 2019/10/17 一.PyTorch虚拟环境配置 该部分操作均在终端( ...
- MOOC C++笔记(七)输入输出流
输入输出流 与输入输出流操作相关的类 istream:是用于输入的流类,cin就是该类的对象. ostream:是用于输出的流类,cout就是该类的对象. ifstream:是用于从文件读取数据的类. ...
- Activity 学习(一) 插件安装篇
目录 Ider下安装 Eclipse下安装 Ider安装图解 首先,创建一个普通的Java工程即可,然后按照下面流程进行: 1:点击菜单中的File(最左上角),选择settings 2:plugin ...
- Spring框架 --- 深入
1.Spring IOC IOC技术: 控制反转,也叫(依赖注入) 控制反转:Bean的生命周期不受你控制,而是交给Spring容器管理. Spring框架如何利用IOC ?: 实现了控 ...
- .net core跨平台应用研究-ubuntu core下配置.net core运行时
引言 年初研究了一阵子.net core跨平台应用,先后发表了几篇应用研究的文章.因工作原因,忙于项目上线,有一阵子没来博客园写文章了.最近项目基本收尾,抽空翻了下自己的博客,廖廖几篇文章,真让人汗颜 ...
- 页面离开前提示用户(onbeforeunload 事件)
window.onbeforeunload = function (e) { var evt = e || window.event; evt.returnValue = '离开会使编写的内容丢失'; ...