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. The Maze Makers(csu1566)

    1566: The Maze Makers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 33[Submit][Status][ ...

  2. JAVA的高并发基础认知 二

    一.JAVA高级并发 1.5JDK之后引入高级并发特性,大多数的特性在java.util.concurrent 包中,是专门用于多线程发编程的,充分利用了现代多处理器和多核心系统的功能以编写大规模并发 ...

  3. PyCharm 添加签名和时间

    工具栏上添加上 Toolbar 点击 Editor -> File and Code Templates -> Python Script 在文本框上填写需要的数据

  4. Layui tree 下拉菜单树

    1.效果: 2.html  代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  5. python之if循环

    if 条件: if语句块else: 语句块 money = int(input("请输入你兜里的钱:")) if money > 500: print("吃肉&qu ...

  6. python自动化开发-5b

    python的常用模块(续) time和datetime模块 time模块和datetime模块举例 例子:获取当前时间 import datetime,time now = time.strftim ...

  7. Oracle 中 流程控制函数 decode用法

    函数介绍 DECODE函数是ORACLE PL/SQL的功能强大的函数之一,目前还只有ORACLE公司的SQL提供了此函数,其它数据库厂商的SQL实现还没有此功能. DECODE有什么用途呢?先构造一 ...

  8. maven(九),install安装到本地仓库

    下载oracle驱动jar包 在maven中央仓库中,是没有oracle驱动jar包的.因为oracle是商业软件,其jar包不允许用作开源用途.从http://www.mvnrepository.c ...

  9. jmeter 压力测试

    转自: https://blog.csdn.net/cbzcbzcbzcbz/article/details/78023327 Jmeter压力测试简单教程(包括服务器状态监控) 2017年09月18 ...

  10. 使用 WebStorm IDE 调试 Pomelo 应用程序

    使用得心应手的IDE来开发应用程序,可以使我们的工作事半功倍.而调试则更可以让我们准确的定位BUG,发现问题.本文讲述如何使用 WebStorm 这个怪兽级JavaScript IDE来调试 Chat ...