1108 Finding Average (20 分)

The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−1000,1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then N numbers are given in the next line, separated by one space.

Output Specification:

For each illegal input number, print in a line ERROR: X is not a legal number where X is the input. Then finally print in a line the result: The average of K numbers is Y where K is the number of legal inputs and Y is their average, accurate to 2 decimal places. In case the average cannot be calculated, output Undefined instead of Y. In case K is only 1, output The average of 1 number is Y instead.

Sample Input 1:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

Sample Output 1:

ERROR: aaa is not a legal number
ERROR: 9999 is not a legal number
ERROR: 2.3.4 is not a legal number
ERROR: 7.123 is not a legal number
The average of 3 numbers is 1.38

Sample Input 2:

2
aaa -9999

Sample Output 2:

ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined

分析:字符串水题

 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-26-19.44.56
 * Description : A1108
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;

 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     ;
     cin>>n;
     string str;
     double ans=0.0;
     ;i<n;i++){
         cin>>str;
         int len=str.length(),j;
         bool flag=false;
         ;j<len;j++){
             if(str[j]=='-') continue;
             if(str[j]=='.'&&flag==false){
                 flag=true;
                 >){
                     printf("ERROR: %s is not a legal number\n",str.c_str());
                     break;
                 }
             }
             '))){
                 printf("ERROR: %s is not a legal number\n",str.c_str());
                 break;
             }
         }
         if(j==len){
             double temp=stod(str);
              &&temp<=){
                 ans+=temp;
                 num++;
             }
             else{
                 printf("ERROR: %s is not a legal number\n",str.c_str());
             }
         }
     }
     ){
         printf("The average of %d numbers is %.2f\n",num,ans/num);
     }
     ){
         printf("The average of 0 numbers is Undefined\n");
     }
     ){
         printf("The average of %d number is %.2f\n",num,ans/num);
     }
     ;
 }
 

1108 Finding Average (20 分)的更多相关文章

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

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

  2. PAT Advanced 1108 Finding Average (20 分)

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

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

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

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

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

  5. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

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

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

  7. pat 1108 Finding Average(20 分)

    1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...

  8. PAT 1108 Finding Average [难]

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  9. 1108 Finding Average

    题意:根据条件判定哪些数是合法的,哪些是不合法的.求其中合法的数的平均值. 思路:字符串处理函数,考虑到最后输出的时候需要控制格式,因此选用scanf()和printf().另外需要了解atof()函 ...

随机推荐

  1. L1-012 计算指数

    真的没骗你,这道才是简单题 —— 对任意给定的不超过 10 的正整数 n,要求你输出 2​n​​.不难吧? 输入格式: 输入在一行中给出一个不超过 10 的正整数 n. 输出格式: 在一行中按照格式  ...

  2. 对小波变换中DWT和CWT的理解

    转载自 http://blog.sina.com.cn/s/blog_633750d90100hbco.html 连续小波变换的概念.操作.及时间尺度图的显示 最近很多网友问到关于连续小波变换的诸多问 ...

  3. spring集成redis——主从配置以及哨兵监控

    Redis主从模式配置: Redis的主从模式配置是非常简单的,首先我们需要有2个可运行的redis环境: master node : 192.168.56.101 8887 slave node: ...

  4. 生成banner的网站

    http://patorjk.com/software/taag 例如: .__ .__ .__ .__ .___ | |__ ____ | | | | ______ _ _____________| ...

  5. 第二篇 makefile的基本结构

    makefile用于定义并描述源文件之间的依赖关系,用于说明如何编译各个源文件并生成最终的可执行文件,规则是makefile中的基本组成部分. 一个最基本的依赖规则如下所示: targets代表这个规 ...

  6. CentOS7安装OpenStack(Rocky版)-09.安装Cinder存储服务组件(控制节点)

    本文分享openstack的Cinder存储服务组件,cinder服务可以提供云磁盘(卷),类似阿里云云盘 ----------------------- 完美的分隔线  -------------- ...

  7. C/C++ 字符串分割: strtok 与 strsep 函数说明

    函数原型: char *strtok(char *s, const char *delim); char *strsep(char **s, const char *delim); 功能:strtok ...

  8. Photoshop通道抠出散乱的儿童头发

    抠图之前仔细分析是必不可少的.要了解清楚需要抠取部分的构成,然后选择最快捷的方法.教程素材图片人物头发色调比较单一,背景色也比较单一,用通道抠图是非常快捷的. 最终效果1 最终效果2 原图 一.复制图 ...

  9. 网络流初步:<最大流>——核心(增广路算法)(模板)

    增广路的核心就是引入了反向边,使在进行道路探索选择的时候增加了类似于退路的东西[有一点dp的味道??] 具体操作就是:1.首先使用结构体以及数组链表next[ MAXN ]进行边信息的存储 2.[核心 ...

  10. 动态绑定AJAX,获取下级分类并延迟执行

    HTML: <div id='allType'> <div class='allTypeHead'><span>所有分类</span></div& ...