题意:

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

trick:

测试点2答案错误原因:题面指出如果只有一个合法数字,输出numbers的时候不加s,即为number。

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int main(){
//ios::sync_with_stdio(false);
//cin.tie(NULL);
//cout.tie(NULL);
int n;
cin>>n;
int cnt=;
double sum=;
for(int i=;i<=n;++i){
string s;
cin>>s;
int flag=;
if(s[]=='-')
flag=;
double x=;
int pos=-;
for(int i=flag;i<s.size();++i){
if(s[i]=='.'){
pos=i;
break;
}
if(s[i]<''||s[i]>''){
flag=;
break;
}
x*=;
x+=s[i]-'';
}
if(pos!=-)
for(int i=pos+;i<s.size();++i){
if(s[i]<''||s[i]>''){
flag=;
break;
}
x+=(s[i]-'')/(1.0*pow(,(i-pos)));
}
if(flag)
x*=-;
if(s.size()--pos>&&pos!=-)
flag=;
if(flag==&&pos==||flag==&&pos==)
flag=;
if(x>||x<-)
flag=;
if(flag==){
cout<<"ERROR: "<<s<<" is not a legal number\n";
continue;
}
++cnt;
sum+=x;
}
sum/=1.0*cnt;
cout<<"The average of "<<cnt;
if(cnt!=)
cout<<" numbers is ";
else
cout<<" number is ";
if(!cnt)
cout<<"Undefined";
else
printf("%.2lf",sum);
return ;
}

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

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

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

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

  4. PAT 甲级 1035 Password (20 分)

    1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...

  5. PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)

    1073 Scientific Notation (20 分)   Scientific notation is the way that scientists easily handle very ...

  6. PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)

    1050 String Subtraction (20 分)   Given two strings S​1​​ and S​2​​, S=S​1​​−S​2​​ is defined to be t ...

  7. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  8. PAT 甲级 1042 Shuffling Machine (20 分)(简单题)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

  9. PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

    1041 Be Unique (20 分)   Being unique is so important to people on Mars that even their lottery is de ...

随机推荐

  1. ASP.NET Razor 常用示例

    1.在网页中显示@符号 使用@@即可使编译器不切换到c#,这样在网页中会显示一个@符号. 2.隐式表达式 也就是正常的razor语法,不能包含空格.(除了await 如:<p>@await ...

  2. numpy学习(三)

    练习篇(Part 3) 31. 略 32. Is the following expressions true? (★☆☆) np.sqrt(-1) == np.emath.sqrt(-1) prin ...

  3. .net Core 安装在linux上

    1.安装 .net Core 参考官方网站 https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install 2.发布应用程 ...

  4. 启动Hive时报错(com.mysql.jdbc.Driver") was not found in the CLASSPATH)

    这是因为没有mysql-connector的jar包.需要把jar包复制到hive目录lib文件夹中. 参考博客:https://blog.csdn.net/Realoyou/article/deta ...

  5. MapReduce第一个项目 WordCount

    参考自林子雨大数据教学:     http://dblab.xmu.edu.cn/blog/hadoop-build-project-using-eclipse/ 创建一个文件夹:放入一个文本文件:填 ...

  6. 搭建 Review Board - SVN 审核工具

    一.安装环境 CentOS-6.7,ReviewBoard-2.5.1.1 二.安装环境的配置 1.确认当前系统中有如下包,若没有,使用yum安装 httpd-2.2.15:httpd 指的是apac ...

  7. sql server和my sql 命令(语句)的区别,sql server与mysql的比较

    sql与mysql的比较 1.连接字符串sql  :Initial Catalog(database)=x;  --数据库名称      Data Source(source)=x;        - ...

  8. git merge代码

    把主干合并进分支 git checkout branch git merge origin/master [一开始我使用的是git merge master,提交代码后发现主干和分支还是有差异,如果不 ...

  9. IIS学习笔记

    IIS传输笔记 1.作用 IIS 将网站开发代码传输到服务器上,就是为了网站的发布 2.下载安装 我是使用的云服务器,windows sever 2012 2.1打开"服务器管理器" ...

  10. SQLserver各种时间取值格式

    叫大哥 SQLServer提取日期中的年月日及其他格式 SQLServer提取日期中的年月日及其他格式   提取年:datepart(yy,getdate())提取月:datepart(mm,getd ...