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. ORM对象关系映射之GreenDAO源码解析

    上一篇我们学习了GreenDAO的CRUD基本操作,可以说是非常的方便的,而且GreenDAO的效率和性能远远高于其它两款流行的ORM框架,下面是我从官网找的一副它们三个ORM框架之间的性能测试的直观 ...

  2. GRUB与Linux系统修复(第二版)

    GRUB配置解析 配置文件保存在 /boot/grub/grub.conf[/boot分区最好应该单独划分出来] 软链接保存在 /etc/grub.conf 1.grub.conf文件分析 defau ...

  3. SpriteBuilder中锚点的一般用法

    注意:改变节点的锚点(anchor point)将会影响缩放和旋转操作,也会影响边界边框和碰撞的检测. 锚点仅仅挪动节点的视觉表现,这种改变可能与物理表现不一致. 你绝不应该错误的挪动锚点去改变节点的 ...

  4. 漫谈jdbc

    本文可作为北京尚学堂jdbc课程的学习笔记; 简介 jdbc是什么东西? jdbc全称(Java Database Connectivity java数据库连接) 它是干什么的? 至于它是干什么的,那 ...

  5. 【Android 应用开发】BluetoothSocket详解

    一. BluetoothSocket简介 1. 简介 客户端与服务端 : BluetoothSocket 和 BluetoothServerSocket 类似于Java中的套接字的 Socket 和 ...

  6. 单片机驱动AT24C02存储芯片

    AT24C02是一个2K位串行CMOS E2PROM, 内部含有256个8位字节,CATALYST公司的先进CMOS技术实质上减少了器件的功耗.AT24C02有一个8字节页写缓冲器.该器件通过IIC总 ...

  7. Dll的编写 在unity中加载

    1. 在VS中新建Dll项目 2.在头文件中对函数进行声明 extern "C" int _declspec(dllexport) testunity(); 3.在源文件中写函数体 ...

  8. 基于condition 实现的线程安全的优先队列(python实现)

    可以把Condiftion理解为一把高级的琐,它提供了比Lock, RLock更高级的功能,允许我们能够控制复杂的线程同步问题.threadiong.Condition在内部维护一个琐对象(默认是RL ...

  9. shim & polyfill

    在JavaScript中,经常提到shim和polyfill,polyfill是shim的一种.shim 是将不同 api 封装成一种,比如 jQuery 的 $.ajax 封装了 XMLHttpRe ...

  10. Populating Next Right Pointers in Each Node(I and II)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...