题意:

输入一个正整数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. Eclipse创建JSP、HTML、CSS文件默认字符集设置成UTF-8

    问题:在使用eclipse的时候总是发现新创建的JSP文件.HTML文件等默认总是ISO-8859-1,每次都要修改成自己使用的utf-8的,很是麻烦,因此在网上查看了一下发现是可以修改字符集的默认值 ...

  2. 腾讯云COS对象存储

    一.腾讯云COS 腾讯云对象存储 COS 是一种存储海量数据的分布式存储服务.COS 提供了多种对象的存储类型:标准存储.低频存储.归档存储. 二.为什么要使用TA 便宜: 个人用户有6个月的免费使用 ...

  3. JavaScript的jQuery

    JavaScript的jQuery 不通过JavaScript的原生代码,如document.getElementById("") 而是通过jQuery的$符号选择器. jQuer ...

  4. Postman:下载安装与基本介绍

    1.下载: (1)官网APP: https://www.getpostman.com/ (即: https://app.getpostman.com/app/download/win64 ) (2)插 ...

  5. 《深入理解Java虚拟机》读书笔记九

    第十章 早期(编译期)优化 1.Javac的源码与调试 编译期的分类: 前端编译期:把*.java文件转换为*.class文件的过程.例如sun的javac.eclipseJDT中的增量编译器. JI ...

  6. 如何在vivado中调用ultraedit 编辑器

    ISE下点击菜单Edit -> Preferences -> Editor. 在Editor选项框里选择Custom,在Command line syntax文本框里输入: {C:/Pro ...

  7. (转)http 之session和cookie

    http://www.cnblogs.com/xuxm2007/archive/2011/12/05/2276705.html Session简介 摘 要:虽然session机制在web应用程序中被采 ...

  8. JMeter压力测试,http请求压测,5分钟让你学会如何压测接口!

    JMeter压力测试 官网:https://jmeter.apache.org 最新款的jmeter需要java8的支持,所以请自行安装jdk8.这里就不啰嗦了. 可以根据自己的系统下载zip或者是t ...

  9. ET框架之自写模块SmartTimerModule

    1.代码结构图 2.SmartTimer 模块Entity: using System; namespace ETModel { [ObjectSystem] public class SmartTi ...

  10. Django REST framework快速入门(官方文档翻译翻译)

    开始 我们将创建一个简单的API来允许管理员用户查看和编辑系统中的用户和组. 项目设置 创建一个新的django项目,命名为:<tutorial>,然后创建一个新的应用程序(app),命名 ...