【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 ...
随机推荐
- 在npm上如何发布自己的包
环境需要:node和npm 然后在本地建一个文件夹作为你要发布的模块 然后在这个文件夹下打开cmd 执行:npm init 按照提示写对应的,或者先不写也可以,最后y 这时候在mcrm这个文件夹下就出 ...
- Oracle 中的 Incarnation 到底是个什么?概念理解篇
高中时候,我深深“爱”上了一位女孩子.那个年纪确实不懂什么是真正的“爱”,反正每天满脑子都是她,只要见到她就会紧张和激动,确切的说是深深的喜欢.你告诉我这叫初恋?不,我的初恋应该是小学3年级,三六班. ...
- js前端模块化的前世今生
前言: <!DOCTYPE html> <html> <head> <title></title> </head> <sc ...
- 2.4测试赛AC代码临时保存
//H #include<cstdio> #include<cstdlib> #include<cstring> #include<stack> usi ...
- Python RSA
# -*- coding: utf-8 -*- from Crypto import Random from Crypto.Hash import SHA from Crypto.Cipher imp ...
- mybatis(三):框架结构
- ASP.NET MVC入门到精通——MVC请求管道
https://www.cnblogs.com/jiekzou/p/4896315.html 本系列目录:ASP.NET MVC4入门到精通系列目录汇总 ASP.NET MVC的请求管道和ASP.NE ...
- 洛谷P1170 兔八哥与猎人 欧拉函数的应用
https://www.luogu.org/problem/P1170 #include<bits/stdc++.h> using namespace std; ],b[],c[],d[] ...
- MySQL5.5升级至5.7
一.下载免安装MySQL5.7包 下载页面 下载链接 二.替换mysql的安装目录 解压mysql5.7 tar -zxf mysql-5.7.23-linux-glibc2.12-x86_64.ta ...
- centos docker 防火墙设置(多个ip之间互相访问)
[Solution] Update firewall policy 1) Disabled docker rules of iptables --- docker will create ...