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 分)的更多相关文章
- PAT甲级——1108.Finding Average (20分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- 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分)
题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...
- Day 007:PAT训练--1108 Finding Average (20 分)
话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...
- PAT (Advanced Level) 1108. Finding Average (20)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1108. Finding Average (20)-字符串处理
求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...
- pat 1108 Finding Average(20 分)
1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...
- PAT 1108 Finding Average [难]
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- 1108 Finding Average
题意:根据条件判定哪些数是合法的,哪些是不合法的.求其中合法的数的平均值. 思路:字符串处理函数,考虑到最后输出的时候需要控制格式,因此选用scanf()和printf().另外需要了解atof()函 ...
随机推荐
- MyEclipse持续性开发教程:用JPA和Spring管理数据(一)
MyEclipse开年钜惠 在线购买低至75折!立即开抢>> [MyEclipse最新版下载] 本教程介绍了MyEclipse中的一些基于JPA / Spring的功能.有关设置JPA项目 ...
- 部署和编写简单web项目
初学的时候,使用默认的Web应用和默认的Servlet URL.对于正式的应用,则应使用定制的Web应用和URL(在部署描述文件web,xml中指定) 仿照默认Web应用的目录结构,创建目录 HTML ...
- TCP/IP网络知识点总结
学完了计算机网络是时候整理一篇总结了,温故知新.注意:这篇博客很长长长(2.5万字+50图). TCP/IP网络知识点总结 一.总述 1.定义:计算机网络是一些互相连接的.自治的计算机的集合.因特网是 ...
- 深度分析:Android4.3下MMS发送到附件为音频文件(音频为系统内置音频)的彩信给自己,添加音频-发送彩信-接收彩信-下载音频附件-预览-播放(三,接收彩信<2,下载彩信>)
彩信的接收简介: 主要是由应用程序负责从彩信服务中心(MMSC Multimedia Messaging Service Center)下载彩信信息.大致的流程是Frameworks会先发出一条短信, ...
- Mysql命令insert into:向表中插入数据(记录)
insert into命令用于向表中插入数据. insert into命令格式:insert into <表名> [(<字段名1>[,..<字段名n > ])] v ...
- Springboot项目使用aop切面保存详细日志到ELK日志平台
上一篇讲过了将Springboot项目中logback日志插入到ELK日志平台,它只是个示例.这一篇来看一下实际使用中,我们应该怎样通过aop切面,拦截所有请求日志插入到ELK日志系统.同时,由于往往 ...
- C语言中右大括号(})后是否加分(;)号问题
在C语言中定义结构体时,右大括号后是需要加分号的,在C++中使用class关键字定义类时,右大括号后也是需要加分号的,而在定义函数时,右大括号后是不需要加分号的. 对这个问题的解释就是右花括号}能代表 ...
- Android手机使用广播监听手机收到的短信
我们使用的Android手机在收到短信的时候会发出一条系统广播.该条广播中存放着接收到的短信的详细信息.本文将详细介绍如何通过动态注册广播来监听短信. 注册广播有两种方式,一种是动态注册,另一种是静态 ...
- poj-1239(递推关系)好难
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...
- 洛谷 1192:台阶问题(递推,DP)
题目描述 有 N 级的台阶,你一开始在底部,每次可以向上迈最多 K 级台阶(最少 1 级),问到达第 N 级台阶有多少种不同方式. 输入输出格式 输入格式: 两个正整数N,K. 输出格式: 一个正整数 ...