1108. Finding Average (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 思路

逻辑题,注意处理下负数的情况,其余按题目要求设置好if语句的条件就行。 代码
#include<iostream>
#include<iomanip>
#include<string>
using namespace std; bool islegal(string s)
{
int len = s.size();
int dotcount = ;
int i = ;
if(s[i] == '-')
{
if(i + == len)
return false;
i++;
}
for(;i < len;i++)
{
if(s[i] == '.')
{
dotcount++;
if(dotcount > )
return false;
if(len - - i > )
return false;
}
else if(s[i] < '' || s[i] > '')
return false;
}
double num = stod(s);
if(num < - || num > )
return false;
return true;
} int main()
{
int N;
while(cin >> N)
{
string str;
double sum = ;
int cnt = ;
for(int i = ;i < N;i++)
{
cin >> str;
if(islegal(str))
{
cnt++;
sum += stod(str);
}
else
{
cout << "ERROR: " << str << " is not a legal number" << endl;
}
}
if(cnt == )
cout << "The average of "<< cnt <<" numbers is Undefined" << endl;
else if(cnt == )
cout << "The average of 1 number is " << fixed << setprecision() << sum << endl;
else
cout << "The average of "<< cnt <<" numbers is "<< fixed << setprecision() << sum/cnt << endl;
}
}

Pat1108: Finding Average的更多相关文章

  1. 1108 Finding Average (20 分)

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

  2. PAT 1108 Finding Average [难]

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

  3. PAT_A1108#Finding Average

    Source: PAT A 1108 Finding Average (20 分) Description: The basic task is simple: given N real number ...

  4. pat 1108 Finding Average(20 分)

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

  5. 【刷题-PAT】A1108 Finding Average (20 分)

    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. A1108. Finding Average

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

  8. PAT A1108 Finding Average (20 分)——字符串,字符串转数字

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

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

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

随机推荐

  1. 理解WebKit和Chromium: Chromium网络栈

    转载请注明原文地址:http://blog.csdn.net/milado_nju ## 概述 前面讲到Chromium的资源加载机制,在调用栈上,提到URLRequest之后就戛然而止,在这之下就是 ...

  2. mysql进阶(十二)常见错误汇总

    原因:外键名不能重复

  3. android ListView加载不同布局

    今天来跟大家讨论下同一个ListView如何加载不同的布局. 老规矩,先来看效果图. 主要步骤如下 1.增加Type. 2.重写getViewTypeCount方法. 3.重写getItemViewT ...

  4. C语言生成32位和64位随机数算法

    C语言生成32位和64位随机数算法 /** * randstd.h * * Standard definitions and types, Bob Jenkins * * 2015-01-19: re ...

  5. UIScrollView的无限左滑轮播一点也不难

    UIScrollView的轮播在如今的app中用得十分广泛,最初实现的时候方式比较拙劣,滚动到最后一个视图时再返回到第一个看起来非常的不连贯. 今天查询UIScrollView轮播资料,总结两种比较喜 ...

  6. Java IO学习--(四)网络

    Java中网络的内容或多或少的超出了Java IO的范畴.关于Java网络更多的是在我的Java网络教程中探讨.但是既然网络是一个常见的数据来源以及数据流目的地,并且因为你使用Java IO的API通 ...

  7. 使用XStream是实现XML与Java对象的转换(6)--持久化

    九.持久化 在第八节的示例中,当我们操作一组对象时,我们可以指定Writer.OutputStream来写出序列化后的XML数据,我们还可以指定Reader.InputStream来读取序列化后的XM ...

  8. Ajax的简单使用

    仅介绍Ajax的使用,让入门小白快速上手 //请自行引入jQuery库文件 <script type="text/javascript"> $(function() { ...

  9. 只需几分钟跟小猫学前端(内含视频教程):nodejs基础之用express、ejs、mongdb建设简单的网站

    开门见山视频教程 https://v.qq.com/x/page/d0645s79xrq.html 前 言: 这是小猫的第二篇node教程,第一篇教程是一个简单的试水,小猫的node教程面向对象为没有 ...

  10. python redis模块的常见的几个类 Redis 、StricRedis和ConnectionPool

    日常写代码过程中,经常需要连接redis进行操作.下面我就介绍下python操作redis模块redis中的几个常见类,包括redis连接池. 一.StrictRedis 类 请看代码:. #!/us ...