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 ...
随机推荐
- 杭电20题 Human Gene Functions
Problem Description It is well known that a human gene can be considered as a sequence, consisting o ...
- 转 精选37条强大的常用linux shell命令组合
1 删除0字节文件 find . -type f -size 0 -exec rm -rf {} \; find . type f -size 0 -delete 2 查看进程,按内存从大到小排列 p ...
- Strusts2--课程笔记5
数据验证: 输入验证分为客户端验证与服务器端验证.客户端验证主要通过JavaScript脚本进行,而服务器端验证主要是通过Java代码进行验证. 分为以下四种情况: (1)手工编写代码,对所有Ac ...
- 如何在VS2013中显示代码行号
http://jingyan.baidu.com/article/af9f5a2d2e193543140a4533.html
- Entity Framework技巧系列之十四 - Tip 56
提示56. 使用反射提供程序编写一个OData Service 在TechEd我收到一大堆有关将数据作为OData暴露的问题. 到目前为止你大概知道可以使用数据服务与Entity Framework将 ...
- Underscore.js 的模板功能介绍与应用
Underscore是一个非常实用的JavaScript库,提供许多编程时需要的功能的支持,他在不扩展任何JavaScript的原生对象的情况下提供很多实用的功能,需要了解的朋友可以详细参考下 U ...
- Java Web项目发布及使用自定义域名
详细讲解的网址: http://wenku.baidu.com/link?url=-ACZxKUcfrbhrMRUP3Ov-Q_c-Q9JPrA9D1fzHjHuJsWwZfRsVDVQ2qBtoY7 ...
- MySQL性能优化的最佳21条经验【转载】
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...
- TortoiseGit保存用户名密码的方法
方法一: 设置 -> git 编辑本地 .git/config 增加 1 [credential] 2 helper = store 保存,输入一次密码后第二次就会记住密码了 方法 ...
- Glusterfs 分布式存储安装部署
Glusterfs 分布式存储部署 是存储当中可以选择的一种 现在很多虚拟化 云计算都在用软件存储 例如 ceph Glusterfs 等等 今天我们部署一下Glusterfs环境 GlusterFs ...