1108. Finding Average (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 [-1000, 1000] 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 (<=100). 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 思路

逻辑题,注意处理下负数的情况,其余按题目要求设置好if语句的条件就行。 代码
#include<iostream>
#include<iomanip>
#include<string>
using namespace std; bool islegal(string s)
{
int len = s.size();
int dotcount = ;
int i = ;
if(s[i] == '-')
{
if(i + == len)
return false;
i++;
}
for(;i < len;i++)
{
if(s[i] == '.')
{
dotcount++;
if(dotcount > )
return false;
if(len - - i > )
return false;
}
else if(s[i] < '' || s[i] > '')
return false;
}
double num = stod(s);
if(num < - || num > )
return false;
return true;
} int main()
{
int N;
while(cin >> N)
{
string str;
double sum = ;
int cnt = ;
for(int i = ;i < N;i++)
{
cin >> str;
if(islegal(str))
{
cnt++;
sum += stod(str);
}
else
{
cout << "ERROR: " << str << " is not a legal number" << endl;
}
}
if(cnt == )
cout << "The average of "<< cnt <<" numbers is Undefined" << endl;
else if(cnt == )
cout << "The average of 1 number is " << fixed << setprecision() << sum << endl;
else
cout << "The average of "<< cnt <<" numbers is "<< fixed << setprecision() << sum/cnt << endl;
}
}

Pat1108: Finding Average的更多相关文章

  1. 1108 Finding Average (20 分)

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

  2. PAT 1108 Finding Average [难]

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

  3. PAT_A1108#Finding Average

    Source: PAT A 1108 Finding Average (20 分) Description: The basic task is simple: given N real number ...

  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. [转].NET程序破解仅需三步

    近期开发公司商城,为了简化开发用了V5Shop网店程序.本来预计一个月完工,哪知道出现一堆问题大大增加了我的工作量(早知道还不如全部自己写了). 破V5Shop真不地道,说是免费的,结果程序一大堆问题 ...

  2. 一个简单的多机器人编队算法实现--PID

    用PID进行领航跟随法机器人编队控制 课题2:多机器人编队控制研究对象:两轮差动的移动机器人或车式移动机器人研究内容:平坦地形,编队的保持和避障,以及避障和队形切换算法等:起伏地形,还要考虑地形情况对 ...

  3. com.android.dex.DexException: Multiple dex files define(jar包重复引用) 错误解决

    前段时间开始转入Android studio,不料果真使用时候遇到些错误,在此记下! 出现这个错误往往是在libs目录下有个jar包,然后在gradle文件中又引用了,即: 共同引用了. 解决方法: ...

  4. TCP的核心系列 — ACK的处理(一)

    TCP发送数据包后,会收到对端的ACK.通过处理ACK,TCP可以进行拥塞控制和流控制,所以 ACK的处理是TCP的一个重要内容.tcp_ack()用于处理接收到的ACK. 本文主要内容:TCP接收A ...

  5. 增量会话对象——DeltaSession

    在集群环境中为了使集群中各个节点的会话状态都同步,同步操作是集群重点解决的问题,一般来说有两种同步策略,其一是每次同步都把整个会话对象传给集群中其他节点,其他节点更新整个会话对象:其二是对会话中增量修 ...

  6. Google官方网络框架Volley实战——QQ吉凶测试,南无阿弥陀佛!

    Google官方网络框架Volley实战--QQ吉凶测试,南无阿弥陀佛! 这次我们用第三方的接口来做一个QQ吉凶的测试项目,代码依然是比较的简单 无图无真相 直接撸代码了,详细解释都已经写在注释里了 ...

  7. ubuntu安装qq教程

    安装策略是wine+wine QQ TM2013,wine QQ TM2013是一款专门为wine进行优化的版本 我的ubuntu系统是14.04版本,64位 1. sudo apt-get inst ...

  8. Java 必看的 Spring 知识汇总!有比这更全的算我输!

    往 期 精 彩 推 荐    [1]Java Web技术经验总结 [2]15个顶级Java多线程面试题及答案,快来看看吧 [3]面试官最喜欢问的十道java面试题 [4]从零讲JAVA ,给你一条清晰 ...

  9. Spring中对象和属性的注入方式

    一:Spring的bean管理 1.xml方式 bean实例化三种xml方式实现 第一种 使用类的无参数构造创建,首先类中得有无参构造器(重点) 第二种 使用静态工厂创建 (1)创建静态的方法,返回类 ...

  10. 《MySQL必知必会》学习笔记_1

    #选择数据库 USE mysql #返回可用数据库列表 SHOW DATABASES #返回当前数据库中可用表 SHOW TABLES #返回表列 SHOW COLUMNS FROM db #显示特定 ...