Pat1108: Finding Average
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 思路
逻辑题,注意处理下负数的情况,其余按题目要求设置好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的更多相关文章
- 1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- PAT 1108 Finding Average [难]
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- PAT_A1108#Finding Average
Source: PAT A 1108 Finding Average (20 分) Description: The basic task is simple: given N real number ...
- pat 1108 Finding Average(20 分)
1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...
- 【刷题-PAT】A1108 Finding Average (20 分)
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 ...
- A1108. Finding Average
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- PAT A1108 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)-字符串处理
求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...
随机推荐
- October 23, 2013 - Fires and smoke in eastern China
October 23, 2013 - Fires and smoke in eastern China Satellite: Aqua Date Acquired: 10/12/2013 Resolu ...
- tomcat整合apache
历时4个多小时,终于把tomcat与apache整合起来了. 中间出了各种各样的问题,现记录一下,也希望能对后来者有点帮助. 背景 apache与tomcat的区别联系大家都知道: tomcat能处理 ...
- 网站论坛同步用户,整合api,实现…
在网上参考了很多资料后,终于完美实现了网站和discuz!nt论坛的双向整合,整合后网站和论坛之间可以同步注册.登录.退出和修改登录密码操作. 本系统的实现形式是新云CMS网站(ASP)和Discuz ...
- /dev/null 2>&1的意思(可以直接参考shell重定向那篇,/dev/null是空设备)
路还长 别太狂 以后指不定谁辉煌 2>&1 和 &> 的解释 Linux的IO输入输出有三类 Standard Input 代码 0 Standard Output 代码 ...
- android wheelview实现三级城市选择
很早之前看淘宝就有了ios那种的城市选择控件,当时也看到网友有分享,不过那个写的很烂,后来(大概是去年吧),我们公司有这么一个项目,当时用的还是网上比较流行的那个黑框的那个,感觉特别的丑,然后我在那个 ...
- Android开发技巧——高亮的用户操作指南
Android开发技巧--高亮的用户操作指南 2015-12-15补记: 发现使用PopupWindow进行遮罩层的显示,在华为P7上会有问题.具体表现为:画出来的高亮部分会偏下.原因为:通过view ...
- makemenuconfig学习
内核配置: make config:基于文本模式的交互式配置 make menuconfig:基于文本模式的菜单型配置 <*>文件经过编译由.c文件到.o文件,最后链接压缩为内核镜像,它存 ...
- ruby中printf "%x"%-4为何会打印开头..
先看一下ruby中printf "%x" % -4的返回结果: irb(main):134:0> printf "%x\n" % -4 ..fc 前面的. ...
- 详解基于vue,vue-router, vuex以及addRoutes进行权限控制
基于vuex, vue-router,vuex的权限控制教程,完整代码地址见https://github.com/linrunzheng/vue-permission-control 接下来让我们模拟 ...
- http 状态表
整理一下xmlHttp.status的值(http 状态表) 状态码 状态码 意义 释义 100 1xx (临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 继续 客户端应当继续发送请 ...