题意:

输入一个正整数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. 【分享】一款特别轻量的gif生成工具

    github链接:https://github.com/NickeManarin/ScreenToGif/releases/tag/2.19.3 超级好用!支持多种方式(摄像头也可√)录制gif

  2. phpstorm更换主题

    打开PhpStorm,File -- Settings -- Editor -- Color Scheme --General选择你喜欢的风格进行更改,选择完成后单击Apply

  3. C++——动态内存分配1

    9.动态内存分配  new 类型名T(初值列表) 其功能是在程序执行期间申请用于存放T类型对象的内存空间,并依初值列表赋以初值,结果值:成功则T类型的指针,指向新分配的内存:失败则为0(null).若 ...

  4. redis 列表类型list

    列表类型(list)1.插入 左侧插入 :lpush key value1 value2 value3... 右侧插入: lpush key value1 value2 value3... 在指定元素 ...

  5. Selenium(Webdriver)自动化测试常问问题

    http://blog.sina.com.cn/s/blog_c189e2590102w3bv.html Selenium(Webdriver)自动化测试常问问题 (1)selenium中如何保证操作 ...

  6. ORA_12514:TNS:listener does not currently know of service requested in connect descriptor

    问题描述 ORA_12514:TNS:listener does not currently know of service requested in connect descriptor 解决方式 ...

  7. Android 验证APK签名对比

    最近OTT制定产品,其中有一条需求是只能安装自己公司签名的APK,所以在网上找了相关资料,最后总结功能实现如下: 1.签名错误码frameworks/base/core/java/android/co ...

  8. adb logcat日志抓取

    adb命令 logcat日志抓取 一.logcat抓log方法:adb logcat命令,可以加条件过滤 1.安装SDK(参考android sdk环境安装) 2.使用数据线链接手机,在手机助手的sd ...

  9. Codeforces Round #609 (Div. 2) A-E简要题解

    contest链接:https://codeforces.com/contest/1269 A. Equation 题意:输入一个整数,找到一个a,一个b,使得a-b=n,切a,b都是合数 思路:合数 ...

  10. Jquery动态改变my97datepicker的日期形式

    先要解绑触发事件: $('#start').unbind('focus'); 然后再绑定触发事件: $('#start').bind('focus',function(){WdatePicker({s ...