PAT (Advanced Level) 1108. Finding Average (20)
简单模拟。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; char s[];
int n;
int num,num2,pos; bool f()
{
int p=;
for(int i=;s[i];i++)
{
if(s[i]>=''&&s[i]<='') continue;
else if(i==&&s[i]=='-') continue;
else if(s[i]=='.') {p++; continue;}
else return ;
} if(p>) return ; pos=strlen(s);
for(int i=;s[i];i++) if(s[i]=='.') pos=i; int hh=strlen(s)-pos-;
if(hh>) return ; num=;
for(int i=;i<pos;i++)
{
if(s[i]>=''&&s[i]<='')
num=num*+s[i]-'';
if(num>) return ;
}
if(num>) return ; num2=;
for(int i=pos+;s[i];i++)
num2=num2*+s[i]-''; double a=1.0*num+num2/(pow(10.0,strlen(s)-pos-));
if(a>1000.0) return ; return ;
} int main()
{
scanf("%d",&n);
double sum=;
int geshu=;
for(int i=;i<=n;i++)
{
scanf("%s",s);
if(f()==)
printf("ERROR: %s is not a legal number\n",s);
else
{
double a=;
a=1.0*num+num2/(pow(10.0,strlen(s)-pos-));
if(s[]=='-') a=-a;
sum=sum+a;
geshu++;
}
}
if(geshu==)
printf("The average of 1 number is %.2lf\n",sum);
else if(geshu==)
printf("The average of 0 numbers is Undefined\n");
else
printf("The average of %d numbers is %.2lf\n",geshu,sum/geshu);
return ;
}
PAT (Advanced Level) 1108. Finding Average (20)的更多相关文章
- 【PAT甲级】1108 Finding Average (20分)
题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642
PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...
- PAT Advanced 1108 Finding Average (20 分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- PAT甲级——1108.Finding Average (20分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- PAT甲题题解-1108. Finding Average (20)-字符串处理
求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...
- Day 007:PAT训练--1108 Finding Average (20 分)
话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...
- PAT (Advanced Level) Practice 1035 Password (20 分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
- 【PAT Advanced Level】1008. Elevator (20)
没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...
随机推荐
- ubuntu 12.04添加桌面启动器
Ubuntu 12.04版本上,无法通过桌面右键菜单建立应用程序启动器:这里参考一个网上方法进行了建立: ubuntu 12.04中,每个应用程序启动器都对应 /user/share/applicat ...
- Windows下PHP(Thread Safe与Non Thread Safe)版本说明
转载“http://www.taoz11.com/archives/300.html” linux下直接下载源码,在服务器上编译即可,发现windows下有4个版本: VC9 x86 Non Thre ...
- MyBatis 批量修改记录
<insert id="update" parameterType="java.util.List"> UPDATE setting SET con ...
- Hash算法冲突解决方法分析
采用开放定址法处理散列表的冲突时,其平均查找长度? 高于链接法处理冲突 低于二分查找 开放定址法:一旦发生冲突,就去寻找下一个空的散列地址,只要散列地址够大,空的地址总会找到 链地址法: 一旦发生冲 ...
- 阅读http://zh.lucida.me/有感
lucida大神本科毕业于大工,研究生毕业于北航,有这样的学历在社会上混就已经绰绰有余了,但是lucida神并不满足,刻苦努力,拼搏进取,最后进入google london工作,曾经一度在micros ...
- Java语言进阶过程(转)
[以下肯定是不完整的列表,欢迎补充] Java是一个通用的编程语言,其实可以干很多事,怎么学Java就看怎么用了. 但有一些一般的步骤: 1. 熟悉一种文本编辑器,比如Vim, Emacs, Note ...
- Visual Studio中使用Git Flow
在VS下使用 GitFlow管理项目开发 1.右键将你的解决方案添加到源代码管理,如果你的VS没有安装git,会提示安装,安装完成之后,在团队资源管理可以看到如下界面 (图一) 2.安装gitflow ...
- C语言_函数【转】
引用地址:http://baike.baidu.com/link?url=U9h6MccLYX2w5uyVOqIFd3eps5gR2FZA10jYRLRnc66Ff_F5ZrmXGKA12DT-_2x ...
- MyEclipse的Expressions没有结果的解决办法
之前我的Expressions在Value这一列什么都不显示,就连简单的1+2结果3都不显示出来. 然后我咬咬牙把它卸载了,然后重装就好了,我也不清楚是什么原因. 1.之前我安装的目录是"C ...
- 导航条css实现和table实现
导航条式样 <style type="text/css"> ul,li{ margin:0; padding:0; list-style:none; } #navtop ...