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. 【Qt编程】基于Qt的词典开发系列<六>--界面美化设计

    本文讲一讲界面设计,作品要面向用户,界面设计的好坏直接影响到用户的体验.现在的窗口设计基本都是扁平化的,你可以从window XP与window 8的窗口可以明显感觉出来.当然除了窗口本身的效果,窗口 ...

  2. Android使用Canvas实现跑马灯

    网上的很多的教程都是通过更改TextView的属性进行跑马灯的设计.这样做有很多的缺点: 1.如果TextView没有获取焦点,那么跑马灯的效果无法实现. 2.如果文本长度小于TextView的宽度, ...

  3. 【Android 应用开发】Android中使用ViewPager制作广告栏效果 - 解决ViewPager占满全屏页面适配问题

    . 参考界面 : 携程app首页的广告栏, 使用ViewPager实现        自制页面效果图 : 源码下载地址: http://download.csdn.net/detail/han1202 ...

  4. HTTPSQS 队列

    http://blog.csdn.net/21aspnet/article/details/7467812 http://hi.baidu.com/caoxin_rain/item/5282770cd ...

  5. DEVICE_ATTR

    说道sysfs接口,就不得不提到函数宏 DEVICE_ATTR,原型是 #define DEVICE_ATTR(_name, _mode, _show, _store) \ struct device ...

  6. 导入android SlidingMenu 应用

    SlidingMenu is a helpful Android library for developers. It creates a side navigation like the Faceb ...

  7. 【Django】 gunicorn部署纪要

    使用Gunicorn 来部署Django应用, 没有一步一步写怎么操作,简单记录下重要的点,方面以后查阅. 主要的方式还是Nginx反向代理到Gunicorn, Gunicorn wsgi来启动Dja ...

  8. javascript语言扩展:可迭代对象(5)

    文章1-4篇说的都是js中的可迭代对象,下面让我们看看ruby中的等价物. 不可否认,ruby中对于迭代器和生成器的语法都相当简洁:ruby从一开始就有一个简洁的基因,而js后来的不断扩充使得其有些语 ...

  9. ruby:借助第三方类名如何查找第三方gem名称(zlib为例)

    rubygem中含有成千上万的第三方gem,网上书上扩展教程中都有指导如何使用第三方gem的例子.但是如果不幸这些例子都没有提及gem名称的话,如何只凭第三方类名或require名查找gem名称呢?换 ...

  10. Django(三)runserver 命令源码分析

    应用环境 windows7 pycharm2018 profession python3.6 django2.0 我们在pycharm 启动django项目时,常常有这么一个命令操作: python ...