简单模拟。

#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)的更多相关文章

  1. 【PAT甲级】1108 Finding Average (20分)

    题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...

  2. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  3. PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...

  4. PAT Advanced 1108 Finding Average (20 分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  5. PAT甲级——1108.Finding Average (20分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  6. PAT甲题题解-1108. Finding Average (20)-字符串处理

    求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...

  7. Day 007:PAT训练--1108 Finding Average (20 分)

    话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...

  8. PAT (Advanced Level) Practice 1035 Password (20 分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  9. 【PAT Advanced Level】1008. Elevator (20)

    没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...

随机推荐

  1. 获取android源码中遇到的问题

    最近项目相当紧张,回家之后还需要继续研究android源码. 从网上找了一些资料,下载过程中还是遇到了很多问题.这里罗列一下: 1. 下载Repo过程中提示证书问题: curl https://dl- ...

  2. php 生成json格式的数据

    放到引入的公共函数里边 if (!function_exists('format_json')) { /** * 格式化API输出的json * @param $return_code string ...

  3. Android自动测试之Monkey工具

    Monkey工具 前言: 最 近开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括android测试框架.CTS.Monkey. Monkeyrunner. ...

  4. Apriori算法-数组-C语言

    原文地址:http://blog.csdn.net/liema2000/article/details/6118423 #include<stdio.h>typedef struct { ...

  5. Lambda表达式介绍

    Lambda表达式实际上是一个匿名函数.它包含表达式和语句,常用于创建委托或表达式目录树类型.所有Lambda表达式都是用Lambda运算符----------  =>,该运算符读为" ...

  6. html 时间单位

    <style>h1{font-size:16px;} .test{position:absolute;left:8px;width:200px;height:100px;margin:0 ...

  7. linux tar包追加问题【转】

    只能已归档的文件才能追加文件. 如果tar.gz文件是如此生成:#tar -zcvf test.tar.gz  a.txt即tar.gz是压缩(-z)和归档(-c)文件,则无法给它追加文件:若果tar ...

  8. laravel5.3初体验

    composer中已经推出了laravel5.3版本的安装依赖. 看到很多诱人的更新,今天决定尝试一下. 背景 操作系统:windows7 php:5.5.37 composer:1.1.3 1.首先 ...

  9. docker install for centos7

    CentOS Docker runs on CentOS 7.X. An installation on other binary compatible EL7 distributions such ...

  10. mongodb 慢SQL查询

    在 MySQL中,慢查询日志是经常作为我们优化数据库的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是Mongo Database Profiler.不仅有,而且还有一些比MySQL ...