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 <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
int n;
double res=;
bool isint(char c){
if(c>='' && c<='')return true;
else return false;
}
bool isnum(string s){
res=;
if(s[]!='-' && s[]!='+' && (s[]<'' || s[]>'')) return false;
int flag=;
if(s[]=='-'){
flag=-;
s.erase(,);
}
else if(s[]=='+'){
s.erase(,);
}
int fp=,np=;
for(int i=;i<s.length();i++){
if(isint(s[i])){
res=res*+s[i]-'';
}
else if(s[i]=='.' && np==){
np++;
fp=i;
}
else{
flag=;
break;
}
}
if(flag==)return false;
if(np==){
int xiao = s.length()-fp-;
if(xiao>) return false;
res = res / pow(,xiao);
}
if(res>) return false;
//printf("%f %d\n",res,flag);
res = res*flag;
//printf("%f %d\n",res,flag);
return true;
} int main(){
scanf("%d",&n);
int cnt=;
double total=;
for(int i=;i<=n;i++){
string s;
cin>>s;
//res=0;
if(isnum(s)){
cnt++;
total = total + res;
//printf("cnt: %d total: %f\n",cnt,total);
}
else{
printf("ERROR: %s is not a legal number\n",s.c_str());
}
}
if(cnt==)printf("The average of 0 numbers is Undefined\n");
else if(cnt!=)printf("The average of %d numbers is %.2f\n",cnt,total/cnt);
else printf("The average of %d number is %.2f\n",cnt,total/cnt);
}

注意点:字符串转数字的判断,按条件一个个判断就好了,注意输入输出时的double要写对。测试点3是只有一个数的情况,1个数时number没有s,没注意所以错了

PAT A1108 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甲级】1108 Finding Average (20分)

    题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...

  3. PAT Advanced 1108 Finding Average (20 分)

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

  4. Day 007:PAT训练--1108 Finding Average (20 分)

    话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...

  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 1108 Finding Average(20 分)

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

  7. PTA 说反话-加强版(20 分)(字符串处理)

    说反话-加强版(20 分) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过500 000的字符串.字符串由若干单词和若干空 ...

  8. PAT 1108 Finding Average [难]

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

  9. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

随机推荐

  1. 【nginx】详细配置说明

    ———————————————————————相关文章———————————————————————————— Centos之安装Nginx及注意事项 [Linux]nginx常用命令 ——————— ...

  2. 项目启动时发生NOT found

    一直想记录一下这个小问题 情景: 我昨晚美滋滋的做完功能,测了测没bug提交到git上之后就屁颠屁颠的回家了,结果今天早上来就失了智,git pull拉了一下代码后,一运行,我去,我的页面呢,页面上直 ...

  3. vue-cli脚手架之webpack.base.conf.js

    webpack相关的重要配置文件将在这一节给出.webpack水很深啊^o^,在此先弄清楚原配文件内容的含义,后续可以自己根据实际情况配置. webpack.base.conf.js:配置vue开发环 ...

  4. drupal7常用函数

    1.获取当前启用的管理员主题名称: $admin_theme = variable_get('admin_theme');

  5. IE和Chrome执行javascript对鼠标双击事件的不同响应

    最近在用天地图API帮同学做点开发的工作,主要是基于天地图的API实现违法用地举报的在线地图标绘,要实现的效果如下: 由于是基于天地图API的TPolygonTool工具实现面积量测和多边形绘制功能, ...

  6. 【转】Maya Mel – Search String in String

    转自:http://schiho.com/?p=179 Hi, this is a little script which returns a 0 or 1 if the searched text ...

  7. android开发笔记:Handler、Looper、MessageQueen、Message的关系

    一.什么是handler? 注:线程分为主线程(主线程又叫UI线程,只能有一个主线程)和子线程(可以有多个)Handler只能在主线程里运行 handler是Android给我们提供用来更新UI的一套 ...

  8. system.img镜像转换为system.new.dat + system.transfer.list

    android 8.1上面验证,支持所有的android版本,直接放到sdk中执行即可.   img2sdat.py #!/usr/bin/env python #coding=utf-8   imp ...

  9. OneAPM大讲堂 | Metrics, Tracing 和 Logging 的关系

    [编者按]这是在 OpenTracing 和分布式追踪领域内广受欢迎的一片博客文章.在构建监控系统时,大家往往在这几个名词和方式之间纠结. 通过这篇文章,作者很好的阐述了分布式追踪.统计指标与日志之间 ...

  10. 洗礼灵魂,修炼python(38)--面向对象编程(8)—从算术运算符进一步认识魔法方法

    上一篇文章了解了魔法方法,相信你已经归魔法方法至少有个概念了,那么今天就进一步的认识魔法方法.说这个之前,大脑里先回忆一下算术操作符. 什么是算术操作符?忘记没有?忘记了的自己倒回去看我前面的博文或者 ...