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. Java 10新特性

    ref:http://www.cocoachina.com/industry/20180309/22520.html https://www.oschina.net/news/94402/java-1 ...

  2. 【工具相关】Web-将网站放在XAMPP上面

    一,将XAMPP服务器打开--->Welcome--->Open Application Folder. 二,会出现如下所示界面.找到htdocs. 三,打开htdocs.如下图所示. 四 ...

  3. java Name [jdbc/myjavadb] is not bound in this Context. Unable to find [jdbc].

    一.出错时的情况: 首先,这是一个servlet项目 1.项目的web.xml配置了:(后来发现不配置这个也行,但是tomcat一定要配置) <resource-ref> <desc ...

  4. 遇到了ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory

    解决方法如下: 1. 通过命令查找libmysqlclient_r.so.16 在什么地方,一般是在/usr/lib64/mysql/下面 2. 做一个链接到/usr/lib64 下: ln -s / ...

  5. “京东金融”主页效果 RecyclerView联动

    先上效果图吧: 第一个想到的实现方式是上面使用horizontalScrollview,下面使用Viewpager,经过尝试之后发现二者API有限,不能达到理想效果.几经折腾,最后上下都使用了自定义的 ...

  6. loadrunner 脚本开发-执行操作系统命令

    脚本开发-执行操作系统命令 by:授客 QQ:1033553122 思路: 用loadrunner system()函数 函数原型: int system( const char *string ); ...

  7. Python tab键命令补全

    pip install pyreadline import rlcompleter, readline readline.parse_and_bind('tab: complete') root@pe ...

  8. g4e基础篇#1 为什么要使用版本控制系统

    g4e 是 Git for Enterprise Developer的简写,这个系列文章会统一使用g4e作为标识,便于大家查看和搜索. 章节目录 前言 1. 基础篇: 为什么要使用版本控制系统 Git ...

  9. Spark性能优化(基于Spark 1.x)

    Task优化:    1.慢任务的性能优化:可以考虑减少每个Partition处理的数据量,同时建议开启spark.speculation(慢任务推导,当检测的慢任务时,会同步开启相同的新任务,谁先完 ...

  10. 前端使用 validate , 根据条件进行动态的验证添加

    需求如下: 审核操作的时候,选择“通过” 就不需要验证审核意见,但是选择的是“不通过”,那么需要进行审核意见验证 <script> $(function () { InitValidate ...