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 ...
随机推荐
- 12C CLONE PDB and config service_listener
Clone PDB PtestDEV to Ptestuat in testuat 1) Clone PtestDEV to Ptestuat C:\Windows\system32> ...
- Thrift源码解析--transport
这一层主要是用于实现网络通信,现在都是基于Tcp/Ip,而Tcp/Ip协议栈由socket来实现,换句话说就是现在网络通信服务底层大都是通过socket实现的,在thrift源码中,就是将socket ...
- 在textarea的内容后面增加内容
document.getElementById("result").value+=data+"\n>"; 本文为本人用来记录自己做的一些东西,如有不对的地 ...
- LYF模板连接.txt
在解决方案里创建了一个新建网站,在其右键下添加一个模板后,准备使用模板的,在添加新建项里突然没有“使用模板页的web窗体”,顿时感觉悲剧... 解决方法:模板页代码---LYFMaterPage.Ma ...
- Tarjan算法求解无向连通图的割点、割边、点双连通分量和边双连通分量的模板
历时好几天,终于完工了! 支持无向图四种功能:1.割点的求解 2.割边的求解 3.点双连通分量的求解 4.边双连通分量的求解 全部支持重边!!!!全部支持重边!!!!全部支持重边!!!! 测试数据: ...
- pig、hive以及hbase的作用
Pig Pig是一种数据流语言,用来快速轻松的处理巨大的数据.Pig包含两个部分:Pig Interface,Pig Latin.Pig可以非常方便的处理HDFS和HBase的数据,和Hive一样,P ...
- python基础(三)列表、数组、字典
列表与元组 列表是最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 1 >>> names = ['wangeq','zlx','jack','rose ...
- 第十节,While循环和for循环
While循环 While循环,是一个循环加判断的组合,满足判断条件返回 真(True)开始循环代码块,不满足判断条件返回 假()不循环 格式: While 条件: 代码块 注意:在While循环里如 ...
- Codeforces Round #350 (Div. 2)_D2 - Magic Powder - 2
D2. Magic Powder - 2 time limit per test 1 second memory limit per test 256 megabytes input standard ...
- C++之文件输入输出
在这里遇见不少的问题,其中的路径问题就是在windows中,\\转义字符才能准确的表示路径 #include <iostream> #include <fstream> #in ...