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 ...
-
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 ...
-
pat 1035 Password(20 分)
1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the ...
随机推荐
-
Python实例1
1.有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 错解: 正解: 源码: #!/usr/bin/python for i in range(1,5): for j in ...
-
loadrunner中创建唯一随机数
常用的时间唯一脚本: Action() { int id, scid; char *vuser_group,strid[5]; char proposalNo[1024]; int i; ...
-
JS——时间日期控件
原文:http://blog.sina.com.cn/s/blog_621768f30100qmfz.html 今天找到一个还不错的日历控件 下载地址:http://www.my97.net/dp/d ...
-
Centos6.4 cobbler安装要点
1,yum 安装cobbler rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm y ...
-
mysql编程---函数
(存储)函数: 函数,也说成"存储函数",其实就是js或php中所说的函数! 唯一的区别: 这里的函数必须返回一个数据(值): 定义形式: 注意事项: 1, 在函数内容,可以有各种 ...
-
腾讯云中ssL证书的配置安装
https://cloud.tencent.com/document/product/619/12797 配置 Nginx 和 HTTPS 完成以上准备工作,就要开始配置 Nginx 和 HTTPS ...
-
Mybatis 系列4
上篇系列3中 介绍了properties与environments, 本篇继续讲剩下的配置节点之一:typeAliases. typeAliases节点主要用来设置别名,其实这是挺好用的一个功能, 通 ...
-
关于TensorFlow你需要了解的9件事
关于TensorFlow你需要了解的9件事 https://mp.weixin.qq.com/s/cEQAdLnueMEj0OQZtYvcuw 摘要:本文对近期在旧金山举办的谷歌 Cloud Next ...
-
atcoder A - Frog 1(DP)
A - Frog 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...
-
Django信息安全相关之CSRF和XSS
什么是xss攻击 xss攻击:黑客把恶意的JS脚本提交到后端,后端把恶意脚本渲染显示出来 什么是CSRF? 1.你登录建行官网后 你的浏览器就保存了建行网站的cokie,(如果不关闭这个页面c ...