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 ...
随机推荐
- __new__和__init__的区别
__new__是一个静态方法,而__init__是一个实例方法. __new__方法会返回一个创建的实例,而__init__什么都不返回. 只有在__new__返回一个cls的实例时后面的__init ...
- php变量详解
变量是用于存储信息的"容器". 定义一个变量的语法: $变量名 = 值; 使用变量的例子: <?php $x=5; $y=6; $z=$x+$y; echo $z; ?> ...
- If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
学习Spring Boot 过程中遇到了下列这个问题 Description: Failed to configure a DataSource: 'url' attribute is not spe ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
今天服务器遇到了一个很熟悉的问题, 输入 #mysql -u root -p ERROR 2002 (HY000):Can't connect to local MySQL server ...
- ansible的playbook简单使用
一.介绍 playbook就是一个用yaml语法把多个模块堆起来的一个文件 核心组件: Hosts:执行的远程主机列表Tasks:任务,由模块定义的操作的列表:Varniables:内置变量或自定义变 ...
- mysql 常用字段类型
tinyint[(m)] [unsigned] [zerofill] 1字节 极小整数,数据类型用于保存一些范围的整数数值范围: 有符号: -128 - 127. 无符号: - 255 特别的: My ...
- spring boot 启动脚本
启动的时候 在 boot_class 中有个:com.sankuai.qcs.regulation.shanghai.App 这是spring boot的配置,在 bin/run_main.sh中 ...
- PHPStorm从入门到精通
1. 使用phpstorm+xdebug进行调试 首先,安装php的xdebug扩展 查看phpinfo中php的版本,php的安装位数,php的是否线程安全:根据这些下载对应的xdebug.dll ...
- TP5上传图片
模板: <form action="{:url('Temp/addTempDo')}" enctype="multipart/form-data" met ...
- Windows Server 2012 IIS 8 - 安装SSL证书
从证书邮件里或者用户中心复制对应的SSL证书文件代码 把代码粘贴到TXT文本文件里面 然后另存为cer或是crt文件,注意编码为ANSI 中级证书和交叉证书也是按以上方法保存为crt或cer文件即可 ...