PAT_A1108#Finding Average
Source:
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 numberwhereXis the input. Then finally print in a line the result:The average of K numbers is YwhereKis the number of legal inputs andYis their average, accurate to 2 decimal places. In case the average cannot be calculated, outputUndefinedinstead ofY. In caseKis only 1, outputThe average of 1 number is Yinstead.
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的更多相关文章
- Pat1108: Finding Average
1108. Finding Average (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The b ...
- 1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- PAT 1108 Finding Average [难]
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- pat 1108 Finding Average(20 分)
1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...
- 【刷题-PAT】A1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- PAT (Advanced Level) 1108. Finding Average (20)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- A1108. Finding Average
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 ...
- PAT甲题题解-1108. Finding Average (20)-字符串处理
求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...
随机推荐
- kendo grid create
这种自定义的create中的函数,这个data的行为是在发送到后端之前执行的 //{ // url: "/admgr/AdUserAuthorityAdd", // dataTyp ...
- sqlServer杂计
In与Exists的区别 这两个函数是差不多的,但由于优化方案不同,通常NOT Exists要比NOT IN要快,因为NOT EXISTS可以使用结合算法二NOT IN就不行了,而EXISTS则不如I ...
- MVC.NET:提供对字体文件.woff的访问
在我们的项目中如果使用到了Bootstrap框架的话,通常都必须要支持对其自带的glyphicons-halflings-regular的font文件的访问. 诸如在MVC.NET中,如果一开始什么设 ...
- PHP array_key_exists()
定义和用法 array_key_exists() 函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 false. 语法 array_key_exists(ke ...
- AWS OpsWorks新增Amazon RDS支持
AWS OpsWorks是一个应用管理服务. 你可以通过它把你的应用在一个 堆栈中定义成为不同层的集合.每一个堆栈提供了须要安装和配置的软件包信息,同一时候也能部署不论什么在OpsWorks层中定义的 ...
- jxl 导入导出Excel(有模板)
1.导入 @Override public String importBusinessScope(File file, String unit_id) throws Exception { Workb ...
- 【Geforce】关于如何在Geforce Experience中登录
相信不少人无法登录这个该死的Geforce Experience.这里提供几个解决方案: 1.在“服务”中启动运行 NVIDIA NetworkService Container 方式改为手动或者自动 ...
- luogu3469 [POI2008]BLO_Blockade
题目大意 给一个无向连通图,求对于每一个点,去掉该点后图中连通结点有序对的减少量. 思路 当时想这道题时,我想到:枚举每一个点,在删去它后连通的几个部分中Dfs得到各个部分的点的个数从而得到解,但是我 ...
- rk3288对于parameter参数文件的解析处理【转】
本文转载自:http://blog.csdn.net/groundhappy/article/details/56479694 rk3288有一个parameter文件. 类似于 FIRMWARE_V ...
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程【转】
本文转载自:http://blog.csdn.net/mu0206mu/article/details/7465603 Android系统Recovery工作原理之使用update.zip ...