A1108. Finding Average
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
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
char num[];
const double INF = 100000000.0;
double prase_(char num[]){
int len = strlen(num);
int pos = -, cnt = ;
int tag = ;
for(int i = ; num[i] != '\0'; i++){
if(!(num[i] >= '' && num[i] <= '' || num[i] == '.' || num[i] == '-')){
return INF;
}
if(num[i] == '.')
cnt++;
if(cnt > )
return INF;
}
if(num[] == '-')
tag = ;
for(pos = ; num[pos] != '.' && num[pos] != '\0'; pos++);
int P = , ansL = , ansR = ;
if(pos == '\0'){
for(int i = len - ; i >= tag; i--){
ansL += (num[i] - '') * P;
P *= ;
}
if(tag == )
return ansL * -1.0;
else return ansL * 1.0;
}else{
if(len - pos - > )
return INF;
P = ; ansL = ;
for(int i = pos - ; i >= tag; i--){
ansL += (num[i] - '') * P;
P *= ;
}
P = ; ansR = ;
for(int i = len - ; i > pos; i--){
ansR += (num[i] - '') * P;
P *= ;
}
double temp = ansR * 1.0;
for(int i = ; i < len - pos - ; i++){
temp *= 0.1;
}
if(tag == )
return ((double)ansL + temp) * -1.0;
else return (double)ansL + temp;
}
}
int main(){
int N, cnt = ;
double sum = , temp = ;
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%s", num);
temp = prase_(num);
if(temp > 1000.0 || temp < -1000.0){
printf("ERROR: %s is not a legal number\n", num);
}else{
sum += temp;
cnt++;
}
}
if(cnt > ){
double prt = sum / (double)cnt;
printf("The average of %d numbers is %.2f\n", cnt, prt);
}else if (cnt == ){
printf("The average of 0 numbers is Undefined\n");
}else if(cnt == ){
double prt = sum / (double)cnt;
printf("The average of %d number is %.2f\n", cnt, prt);
}
cin >> N;
return ;
}
A1108. Finding Average的更多相关文章
- 【刷题-PAT】A1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- PAT A1108 Finding Average (20 分)——字符串,字符串转数字
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- PAT甲级——A1108 Finding Average【20】
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- 1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- Pat1108: Finding Average
1108. Finding Average (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The b ...
- PAT 1108 Finding Average [难]
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- PAT_A1108#Finding Average
Source: PAT A 1108 Finding Average (20 分) Description: The basic task is simple: given N real number ...
- pat 1108 Finding Average(20 分)
1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...
- PAT (Advanced Level) 1108. Finding Average (20)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
随机推荐
- Day 4-1 模块的导入方法和路径
什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...
- python爬虫scrapy之downloader_middleware设置proxy代理
一.背景: 小编在爬虫的时候肯定会遇到被封杀的情况,昨天爬了一个网站,刚开始是可以了,在settings的设置DEFAULT_REQUEST_HEADERS伪装自己是chrome浏览器,刚开始是可以的 ...
- scrapy架构简介
一.scrapy架构介绍 1.结构简图: 主要组成部分:Spider(产出request,处理response),Pipeline,Downloader,Scheduler,Scrapy Engine ...
- CMD & Git Shell & Bash Shell
CMD & Git Shell & Bash Shell https://mvdan.cc/sh/cmd/shfmt PC
- Node & CLI
Node & CLI cli 生成文件的原理是什么 https://nodejs.org/api/cli.html http://nodejs.cn/api/cli.html CLI & ...
- LODOP中page-break-before:always给div分页
Lodop中超过超文本打印项高度会自动分页:Lodop打印控件 超文本自动分页Lodop中还有NewPage和NewPageA,用于手动分页:Lodop强制分页LODOP.NewPage()和LODO ...
- 【NLP】Recurrent Neural Network and Language Models
0. Overview What is language models? A time series prediction problem. It assigns a probility to a s ...
- pycharm中查看源码的快捷键
将光标移动至要查看的方法处,按住ctrl 点击鼠标左键,即可查看该方法的源码
- IntelliJ IDEA通过Tomcat启动项目过程分析
Tomcat部署项目的多种方式 Tomcat安装目录:${TOMCAT_HOME} 1.常规的webapps下直接部署到${TOMCAT_HOME}/webapps下 2.利用控制台进行部署 3.外部 ...
- Django auth认证系统
Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...