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 ...
随机推荐
- spring AOP源码分析(二)
现在,我们将对代理对象的生成过程进行分析. 在springAOP源码分析(一)的例子中,将会生成哪些对象呢? 可以看到将会生成六个对象,对应的beanName分别是: userDao:目标对象 log ...
- laravel添加model文件夹,需要改动的地方
首先,将app\User(等model文件),移入APP\modellists文件夹中,方便整理 第二,修改模型中命名空间和引用其他model的路径 第三,将文件夹app\admin中的控制器文件,全 ...
- springmvc配置文件
1 springMVC的配置文件路径问题 https://www.cnblogs.com/ysloong/p/6071450.html
- Hbase存储模式
以key.value的结构存储数据; (table,rowkey,family,colum,timestamp)构成数据的key,value存储数据
- Golang的日志处理
整个看了一圈下来,感觉Golang的日志包在管理多线程安全的情况下,提供了最小粒度的工具.并没有提供什么复杂的过滤器之类的生成. 实现了一个demo来记录一下日志分类日志打印等实现: package ...
- 在python中定义二维数组
发表于 http://liamchzh.0fees.net/?p=234&i=1 一次偶然的机会,发现python中list非常有意思. 先看一段代码 [py]array = [0, 0, 0 ...
- spring程序打包war,直接通过-jar启动,并指定spring.profiles.active参数控制多环境配置
备注:spring boot有内嵌tomcat,jar项目可以用java -jar命令启动,war包也可以,且可以直接指定spring.profiles.active参数控制多环境配置 直接指定传参, ...
- 使用php导出excel并使用excel的求和统计函数对excel进行汇总
1. 使用excel的统计函数对excel进行多条件汇总求和: =SUMIFS($D$:$D$, $A$:$A$, :$B$, :$C$, "三级片") 例如: =SUMIFS(求 ...
- DBX error:Driver could not be properly initialized .... 解决办法
系统: win7 64位+ MySql 将libmysql.dll和Dbxmys.dll 拷到 C:\Windows\SysWOW64 目录. ( 64位系统) 32位则拷到 c:\wind ...
- codeforces362B
Petya and Staircases CodeForces - 362B 题意: 一个小男孩要上楼梯,他一次可以走1个台阶或2个台阶或3个台阶,但是有一些台阶是脏的,他不想走在脏台阶上.一共有n个 ...