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 分)的更多相关文章

  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. Cocos2d-x 学习笔记(11.1) MoveBy MoveTo

    1. MoveBy MoveTo 两方法都是对node的平移,MoveBy是相对当前位置的移动.MoveTo是By的子类,是移动到世界坐标位置. 1.1 成员变量和create方法 MoveBy的主要 ...

  2. 修改配置文件application.properties

    附录A.常用应用程序属性 可以在application.properties文件内部application.yml,文件内部或命令行开关中指定各种属性.本附录提供了常见Spring Boot属性的列表 ...

  3. 关于javascript闭包的最通俗易懂的理解

    这两天在研究闭包,网上一通找,有牛人写的帖子,有普通人写的帖子,但是大多没戳中本小白所纠结的点,而且大多插入了立即执行函数,其实根本不需要的,反而让人产生了误解.这里我用我的方式讲解一下闭包. 1.目 ...

  4. Java基础(五)继承和多态

    1.多态 先来看一个例子,其中Employee类是父类,Manager类继承了Employee类: public static void main(String[] args) { // constr ...

  5. day3,用户交互,input的应用

    1.与用户交互 输入:input() python2.x版本 input后面家的东西要声明输入的类型       >>> input(">>:")   ...

  6. django-模板之模板变量(二)

    将views中的变量传递给html界面 book/views.py from django.views import View from django.shortcuts import render ...

  7. CentOS 7 的root口令破解两种方法

    破解CentOS7的root口令 方法一: 第一步: 启动时任意键暂停启动 按-e-键进入编辑模式 第二步: 1.将光标移动至蓝框处linux16开头的行,添加内核参数 rd.break 2.按ctr ...

  8. Office中国在这个开个博客

    Office中国在这个开个博客,先来show一下我的网站 Office中国/Access中国  http://www.office-cn.net Office中国百科:   http://baike. ...

  9. C# 计时周期数(Ticks)在不同数据库上的实现

    要在数据库中实现 DateTime.Ticks,先来看看 Ticks 在微软官方文档上的定义: 注解 一个计时周期表示一百纳秒,即一千万分之一秒. 毫秒内有 , 个计时周期,即 秒内有 , 万个计时周 ...

  10. IndentationError: unindent does not match any outer indentation level笔记

    执行一个Python脚本的时候,报"IndentationError: unindent does not match any outer indentation level" 错 ...