【PAT甲级】1108 Finding Average (20分)
题意:
输入一个正整数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分)的更多相关文章
- PAT甲级——1108.Finding Average (20分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- PAT Advanced 1108 Finding Average (20 分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- Day 007:PAT训练--1108 Finding Average (20 分)
话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...
- PAT 甲级 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- PAT 甲级 1073 Scientific Notation (20 分) (根据科学计数法写出数)
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very ...
- PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)
1050 String Subtraction (20 分) Given two strings S1 and S2, S=S1−S2 is defined to be t ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- PAT 甲级 1042 Shuffling Machine (20 分)(简单题)
1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. ...
- PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is de ...
随机推荐
- 2019 LOL 全球总决赛
FPS 牛逼 涅槃重生
- python接口
用正则表达式提取数据: https://www.cnblogs.com/dwdw/p/9553192.html python unittest TestCase间共享数据(全局变量的使用): http ...
- loj6278 数列分块入门题2
题意:支持区间加,询问区间中元素排名 维护两个域.一个域维护原序列,一个域维护快内排序序列. 每次修改后更新快内排序序列. 修改时O(sqrt(n)log(sqrt(n))) 询问时O(sqrt(n) ...
- Selenium3+python自动化009- js之屏幕滑动和日历操作
一.js的滑屏 1)以下脚本实现js滑屏scroll="document.documentElement.scrollTop=800"#垂直滚动 pxscroll = " ...
- linux - 异常:安装包冲突 conflicts with
问题描述 解决方案 删除冲突的包 命令格式:yum -y remove 包名 yum -y remove httpd24u yum -y remove httpd24u-tools
- c语言 printf格式化输出
#include <iostream> #include<stdio.h> #include <cstring> using namespace std; int ...
- mybatis-plus - TableInfo
在前面 的 inject() 方法中, 调用了一个 TableInfoHelper.initTableInfo(builderAssistant, modelClass) 方法, 来获取 表信息: T ...
- ubuntu18+caffe+cuda
昨天安装caffe,因为用的是cuda10.2,遇到各种问题,最终也没有安装成功.使用cmake配置成功.生成成功.编译的时候报错. /usr/local/cuda/include/cuda_runt ...
- [P4549] 【模板】裴蜀定理 - GCD
__gcd真好用 #include <bits/stdc++.h> using namespace std; int main() { int n,x,a=0; cin>>n; ...
- PP: Overviewing evolution patterns of egocentric networks by interactive construction of spatial layouts
Problem: get an overall picture of how ego-networks evolve is a common challenging task. Existing te ...