Source:

PAT A 1108 Finding Average (20 分)

Description:

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 [−] 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 (≤). 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

Keys:

  • 字符串处理
  • string(C++ STL)

Attention:

  • 小数先相加再乘基数,整数先乘基数再相加
  • str.c_str()函数
  • s.size()是unsigned,if(s.size()-pos-1 > 2)这个操作,实际上是无符号整数比较大小,真值的负数会大于正数

Code:

 /*
Data: 2019-06-16 13:51:57
Problem: PAT_A1108#Finding Average
AC: 24:26 题目大意:
计算n个数的平均值,合法数范围在[-1000,1000],且不超过两位小数
*/ #include<cstdio>
#include<string>
#include<iostream>
using namespace std; bool isLegal(string s, double &num)
{
int sign=;
if(s[] == '-')
{
sign=-;
s.erase(,);
}
int pos=s.size();
for(int i=; i<s.size(); i++)
{
if(s[i] == '.')
{
if(pos==s.size())
pos = i;
else
return false;
}
else if(s[i]<'' || s[i]>'')
return false;
}
int len = s.size()-pos-;
if(len > )
return false;
num=;
double upper=,lower=;
for(int i=; i<pos; i++)
{
upper *= ;
upper += (s[i]-'');
}
for(int i=s.size()-; i>pos; i--)
{
lower += (s[i]-'');
lower *= 0.1;
}
num = upper + lower;
if(num > )
return false;
num *= sign;
return true;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,cnt=;
string str;
double sum=,num;
scanf("%d", &n);
for(int i=; i<n; i++)
{
cin >> str;
if(isLegal(str,num))
{
cnt++;
sum += num;
}
else
printf("ERROR: %s is not a legal number\n", str.c_str());
}
if(cnt==)
printf("The average of 0 numbers is Undefined");
else if(cnt==)
printf("The average of 1 number is %.2f", sum);
else
printf("The average of %d numbers is %.2f", cnt,sum/cnt); return ;
}

Update:

 /*
Data: 2019-08-17 20:23:41
Problem: PAT_A1108#Finding Average
AC: 24:26 题目大意:
计算n个数的平均值,合法数范围在[-1000,1000],且不超过两位小数
*/
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
const int Er=1e4; double ToF(string s)
{
int cnt=,acr=;
string x="";
for(int i=; i<s.size(); i++)
{
x += s[i];
if(s[i]=='-' && i==)
continue;
if(cnt)
acr++;
if(s[i] == '.')
{
cnt++;
if(cnt==)
return Er;
}
else if(s[i]<'' || s[i]>'')
return Er;
}
if(acr>)
return Er;
else
return atof(x.c_str());
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,cnt=;
double x=,sum=;
string s;
scanf("%d", &n);
for(int i=; i<n; i++)
{
cin >> s;
x = ToF(s);
if(x>1e3 || x<-1e3)
printf("ERROR: %s is not a legal number\n", s.c_str());
else
{
sum += x;
cnt++;
}
}
if(cnt==)
printf("The average of 0 numbers is Undefined\n");
else if(cnt==)
printf("The average of 1 number is %.2f\n", sum);
else
printf("The average of %d numbers is %.2f\n", cnt,sum/cnt); return ;
}

PAT_A1108#Finding Average的更多相关文章

  1. Pat1108: Finding Average

    1108. Finding Average (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The b ...

  2. 1108 Finding Average (20 分)

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  3. PAT 1108 Finding Average [难]

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  4. pat 1108 Finding Average(20 分)

    1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...

  5. 【刷题-PAT】A1108 Finding Average (20 分)

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  6. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  7. A1108. Finding Average

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  8. PAT A1108 Finding Average (20 分)——字符串,字符串转数字

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  9. PAT甲题题解-1108. Finding Average (20)-字符串处理

    求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...

随机推荐

  1. SpringBoot多数据源改造(二)

    在上一篇的内容中,主要介绍了spring boot项目的多数据源改造的涉及的基本配置及改动.在spring项目中,常用Mybatis做ORM操作数据库,并且分页操作是避免不了的. 因此,这一篇主要介绍 ...

  2. N天学习一个Linux命令之top

    用途 查看机器负载以及进程资源占用情况,linux系统性能分析工具 用法 top -hv | -abcHimMsS -d delay -n iterations -p pid [, pid ...] ...

  3. JEval使用实例

    jeval是为为你的Java应用程序提供可增加的.高性能.数学.  布尔和函数表达式的解析和运算的高级资源包. 以下这个样例包括了JEval经常使用功能: package demo0; import ...

  4. 自己写spring boot starter

    自己写spring boot starter 学习了:<spring boot实战>汪云飞著 6.5.4节 pom.xml <project xmlns="http://m ...

  5. AWS OpsWorks新增Amazon RDS支持

    AWS OpsWorks是一个应用管理服务. 你可以通过它把你的应用在一个 堆栈中定义成为不同层的集合.每一个堆栈提供了须要安装和配置的软件包信息,同一时候也能部署不论什么在OpsWorks层中定义的 ...

  6. 【Discuz】去除版权信息,标题栏与底部改动

    这篇文章尽管是介绍怎么把Discuz!的版权信息怎么搞得无影无踪,可是还是建议在不影响论坛视觉效果的情况下,保留Discuz的版权信息,毕竟它为我奉献了一个这么出色的开源论坛的phpproject.主 ...

  7. Spring+Mybatis之登录功能demo

    其实工作之后就没有用过Spring+Mybatis的框架了,因为公司有一个自己开发的框架,讲道理,其实这个与Spring+Mybatis整合很是神似.当然性能上还是比不上Spring+Mybatis所 ...

  8. SuperSocket内置的命令行协议的解析

    SuperSocket\SocketBase\Protocol\TerminatorReceiveFilter.cs /// <summary> /// Filters received ...

  9. Spring Data 自动生成

    之前一直用的mybatis逆向自动生成,由于最近学习springdata,所以看了一下springdata的自动生成,基本与mybatis一致,不同的也许就是逆向生成代码(实体类,mapper等)和正 ...

  10. npm搭建React项目

    转自:http://blog.csdn.net/u012859720/article/details/70597119 要想使用npm,首先安装Node.js 一.安装全局包 $ npm instal ...