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. [bzoj4010][HNOI2015]菜肴制作_贪心_拓扑排序

    菜肴制作 bzoj-4010 HNOI-2015 题目大意:给定一张n个点m条边的有向图,求一个toposort,使得:(1)满足编号为1的点尽量在前:(2)满足(1)的情况下编号为2的点尽量在前,以 ...

  2. 关于Spring的xml文档的简单实用配置

    Spring的spring.xml文档的配置 最近在写Spring的配置文件时,发现Spring文档的配置其实没必要那么繁琐记忆,网上的很多文章都写得很繁琐,如果所有的东西按照路径去查找,可以很快的帮 ...

  3. oracle latch

    (转载 : http://www.dbtan.com/2010/05/latch-free.html) Latch Free(闩锁释放):Latch Free通常被称为闩锁释放,这个名称常常引起误解, ...

  4. mybatis+mysql返回插入的主键,参数只是提供部分参数

    mybatis+mysql返回插入的主键,参数只是提供部分参数 <insert id="insertByChannelIdOpenid" useGeneratedKeys=& ...

  5. CF799B T-shirt buying

    题目大意 有一些衣服,它们有价格.正面的颜色和反面的颜色.现有一群顾客按顺序来买存在某颜色且价格最低的衣服(不存在则不会买),求每个顾客花了多少钱. 思路 #include <cstdio> ...

  6. vmware上安装ubuntu和vmwaretools

    一.平台:win7操作系统   vmware 10.0.0 ubuntu 14.04 二.vmware下安装ubuntu: 具体安装步骤可以按照推荐的来,但是要注意一定要先创建新的虚拟机,之后再安装u ...

  7. Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决

    Java之POI读取Excel的Package should contain a content type part [M1.13]] with root cause异常问题解决 引言: 在Java中 ...

  8. html5--视频播放器实例

    html5--视频播放器实例 总结: 1.相对定位和绝对定位的区别,两者都是浮起来了 2.属性和方法都是有对象的,搞清楚对象之后,属性和方法就很好用了,我们一般可以用document.getEleme ...

  9. PCB .NET连接MySQL与Oracle DLL文分享件 MySql.Data,Oracle.ManagedDataAccess

    虽然我们C#对SQL SERVER天然的支持,但对于C#要连接MYSQL或Oracle就不同了, 需要用到第3方组件才行,本文将2个组件连接数据库代码与DLL下载地址贴出. 一.C#连接MYSQL   ...

  10. selenium3 + python - select定位

    一.Select模块(index)     1.导入Select模块.直接根据属性或索引定位     2.先要导入select方法:from selenium.webdriver.support.se ...