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 ...
随机推荐
- [NOIp2010] luogu P1514 引水入城
跟 zzy, hwx 等人纠结是否回去上蛋疼的董老板的课. 题目描述 如图所示.你有一个 N×MN\times MN×M 的矩阵,水可以从一格流到与它相邻的格子,需要满足起点的海拔严格高于终点海拔.定 ...
- 1、Struts2基本入门
一.了解了这几个主要的优点,会促使你考虑使用Struts2 : 1.POJO表单及POJO操作 - Struts2 去除掉了Struts框架中的Action Forms部分.在Struts2框架下,你 ...
- java学习5-面向对象(下)
final修饰符: final用于修饰类.变量和方法. final修饰变量时,一旦获得了初始值就不可改变 1.抽象方法和抽象类 抽象方法与抽象类的规则: a.抽象方法和抽象类必须使用abstract修 ...
- date命令查看与修改
在我们使用linux服务器时,肯定会遇到Linux服务器时间不准确的情况如何查看Linux系统的时间,如何修改Linux系统上的当前时间呢. 查看Linux系统当前时间: 命令: date +回车 修 ...
- 百万年薪python之路 -- python2和python3的区别
python2和python3的区别: python2获取的是整数 python3获取的是浮点数 print函数:(Python3中print为一个函数,必须用括号括起来:Python2中print为 ...
- ASP.NET Core中的配置
配置 参考文件点击跳转 配置来源 命令行参数 自定义提供程序 目录文件 环境变量 内存中的.NET 对象 文件 默认配置 CreateDefaultBuilder方法提供有默认配置,在这个方法中会接收 ...
- 06 python学习笔记-常用模块(六)
一. 模块.包 1.什么是模块? Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句,是用来组织代码的.模块能定义函数 ...
- day08整理(周总结\列表\字典内置方法)
一.周总结 一 计算机基础之编程 1.什么是编程语言 编程是人与计算机交流的介质 2.什么是编程 通过编程语言写一堆文件 3,为什么编程 取代劳动力,帮人类干活 二 计算机组成 1.CPU 控制器 控 ...
- OptimalSolution(5)--数组和矩阵问题(2)2
一.找到无序数组中最小的k个数 二.在数组中找到出现次数大于N/K的数 三.最长的可整合子数组的长度 四.不重复打印排序数组中相加和为给定值的所有二元组和三元组 五.未排序正数数组中累加和为给定值的最 ...
- 小程序多端差异调研报告(微信,支付宝,头条,QQ)
已经使用uni-app开发并发布了一个跨端小程序啦,嘻嘻嘻!