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 < ...
随机推荐
- Linux终止进程的工具kill/killall/pkill/xkill/skill用法区别(转)
一. 终止进程的工具kill .killall.pkill.xkill 终止一个进程或终止一个正在运行的程序,一般是通过kill .killall.pkill.xkill等进行.比如一个程序已经死掉, ...
- PHP包管理工具composer简单总结
前言 接触laravel之后,才知道有PSR,composer之类的东西,PHP已经不再是一门草根语言了.最近在尝试玩thrift,需要安装PHP thrift依赖库,使用composer insta ...
- A java code
With the help of LiJun I got a piece of JAVA code. With this code, I can do below things like connec ...
- 2.6-NAT
2.6-NAT 网络地址转换协议NAT(Network Address Translation): 交换和远程都要用,先上什么就放在哪一块讲,具体来说NAT还是属于远程的. ...
- Facebook图搜索unicorn
unicorn(独角兽),里面类似于倒排链的reference list,相应的term如friend:2,表示entity 2的朋友列表,整个结构是shard的,上面是top aggregator, ...
- hdu 1010 Tempter of the Bone (奇偶性剪枝)
题意:有一副二维地图'S'为起点,'D'为终点,'.'是可以行走的,'X'是不能行走的.问能否只走T步从S走到D? 题解:最容易想到的就是DFS暴力搜索,,但是会超时...=_=... 所以,,要有其 ...
- C语言里全局变量管理
C语言里信息封装比較弱,仅仅有静态变量的文件作用域. 假设不加约束.非常easy造成全局变量满天飞. 假设定义一个全局结构体.把全局变量都放到这个GlobleVariate里,应该好管一些,至少比裸奔 ...
- spring mvc文件上传,request对象转换异常
spring 文件上传有现成的工具用起来也挺简单.就是在还不是非常熟悉的时候可能会出一些错. 近期碰到了 org.apache.catalina.connector.RequestFacade can ...
- Android+Jquery Mobile学习系列(8)-保单/生日提醒功能
其实这个App基本功能早已做完,并且交给老婆试用去了.但由于最近项目要保证稳定,所以持续加班,没有时间写最后一点内容,本节也就简单截图做个说明,不详细叙述实现方式.我会把代码上传到最后一章中,有兴趣的 ...
- 查看服务器wwn是否在交换机侧
判断port_state是否为Online状态,是的话,读取出port_name,即为wwn. #!/usr/bin/env python3 # -*- coding: UTF-8 -*- impor ...