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 number
whereX
is the input. Then finally print in a line the result:The average of K numbers is Y
whereK
is the number of legal inputs andY
is their average, accurate to 2 decimal places. In case the average cannot be calculated, outputUndefined
instead ofY
. In caseK
is only 1, outputThe 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的更多相关文章
- 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 < ...
随机推荐
- HDU 5392 Infoplane in Tina Town
Infoplane in Tina Town Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 524288/524288 K (Jav ...
- 关于Windows下程序运行的说明
预计有非常多人首次都是通过Windows(微软的操作系统)来使用计算机的.Windows的设计导致非常多人觉得全部程序仅仅要双击一下就能够被正确运行了,所以一大堆初学程序设计的童鞋就会遇到些疑问: 为 ...
- pytest 失败用例重试
https://www.cnblogs.com/jinzhuduoduo/articles/7017405.html http://www.lxway.com/445949491.htm https: ...
- MySql存储过程与函数
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:存储过程和函数是在数据库中定义一些SQL语句的集合,然后直接调用这些存储过程和函数来运行 ...
- hive学习路线
hive学习路线图:
- silverlight学习笔记——新手对silverlight的认识(1)
这几天在搞silverlight.虽然silverlight没有前途,但始终是微软的一门技术,界面基本上与WPF共通,用一下也无妨. 学习过程并没有我原先想得那么容易,有些地方捣鼓了很久.究其原因,是 ...
- luogu2154 [SDOI2009] 虔诚的墓主人 离散化 树状数组 扫描线
题目大意 公墓可以看成一块N×M的矩形,矩形的每个格点,要么种着一棵常青树,要么是一块还没有归属的墓地.一块墓地的虔诚度是指以这块墓地为中心的十字架的数目,一个十字架可以看成中间是墓地,墓地的正上.正 ...
- Android重力感应器Sensor编程
添加当重力变化时的处理函数在创建监听器时调用的函数 doSomething(x, y, z) 是自己定义的方法.当手机倾斜方向改变时,监听器会调用该方法.我们要做的,就是填充该方法,用于在重力发生变化 ...
- tensorflow 模型压缩
模型压缩 为了将tensorflow深度学习模型部署到移动/嵌入式设备上,我们应该致力于减少模型的内存占用,缩短推断时间,减少耗电.有几种方法可以实现这些要求,如量化.权重剪枝或将大模型提炼成小模型. ...
- CodeForces--626C--Block Towers (二分)
Block Towers Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...