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. 使用emma时遇到的一些问题

    今天在用使用emma的过程中遇到了几个问题,记录一下. 1.跑junit过程中没办法产生coverage data文件,导致最后没办法出emma报告,上官网查了一下原因如下: I have instr ...

  2. tomcat conf目录下文件的作用

    本文以apache-tomcat-6.0.26为准介绍 1.   web.xml: 默认(欢迎)文件的设置  配置包括:主要是配置servlet,包括初始化参数,设置session等 <serv ...

  3. 逆向路由器固件之敏感信息泄露 Part2

    之前的文章中详细介绍了各种解包路由器固件的工具.解包之后就获得了固件中的文件.下一步就是分析文件寻找漏洞了.这次分析的目标是Trendnet路由器,分析的漏洞是一个远程获取路由器权限的漏洞. 初步分析 ...

  4. SWIFT中使用AFNetwroking访问网络数据

    AFNetworking 是 iOS 一个使用很方便的第三方网络开发框架,它可以很轻松的从一个URL地址内获取JSON数据. 在使用它时我用到包管理器Cocoapods 不懂的请移步: Cocoapo ...

  5. IOC和DI 控制反转和依赖注入

    首先要分享的是Iteye的开涛这位技术牛人对Spring框架的IOC的理解,写得非常通俗易懂,以下内容全部来自原文,原文地址:http://jinnianshilongnian.iteye.com/b ...

  6. 判断颜色信息-RGB2HSV

    前言 项目车号识别过程中,车体有三种颜色黑车黑底白字.红车红底白字.绿车黄底绿字,可以通过判断车体的颜色信息,从而判断二值化是否需要反转,主要是基于rgb2hsv函数进行不同颜色的阈值判断. MATL ...

  7. xgboost 简单测试

    #coding=utf8 import pandas as pd from sklearn.model_selection import train_test_split from sklearn.f ...

  8. STM32 输入捕获配置

    在STM32 的定时器,除了 TIM6 和 TIM7,就是通过检测 TIMx_CHx 上的 边沿信号,在边沿信号发生跳变(比如上升沿/下降沿)的时候, 将当时定时器 的值(TIMx_CNT) 存放到对 ...

  9. 20155316 2016-2017-2 《Java程序设计》第5周学习总结

    教材学习内容总结 这周总结 try catch语法 异常继承结构 throw finally AutoCloseable接口 Collection Map Lambda表达式 上周总结 三个关键 类与 ...

  10. ajax 跨域的问题 用js绕过跨域

    之前遇到一个ajax跨域的问题,搜索了很多方法才找到解决方案,也怪自己  菜鸟一个 这些经验还是要慢慢来的 尤其是最近来回顾一下 竟然发现自己忘记了  所以还是把它记下来啊 免得到时候忘记 这个方法原 ...