PAT甲级——A1108 Finding Average【20】
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
#include <iostream>
#include <vector>
using namespace std;
#define inf 1000
double sum = 0.0, number;
int n, nums = ;
int main()
{
cin >> n;
char str1[], str2[];
while (n--)
{
cin >> str1;
bool flag = true;
sscanf(str1, "%lf", &number);//将字符转换为浮点数值
sprintf(str2, "%0.2f", number);//按要求输出
for (int i = ; str1[i] != '\0'&& flag; ++i)
if (str1[i] != str2[i])
flag = false;
if (!flag || number<-inf || number>inf)
cout << "ERROR: " << str1 << " is not a legal number" << endl;
else
{
nums++;
sum += number;
}
}
cout << "The average of " << nums << (nums == ? " number is " : " numbers is ");
if (nums == )
cout << "Undefined" << endl;
else
printf("%0.2f\n", sum / nums);
return ;
}
PAT甲级——A1108 Finding Average【20】的更多相关文章
- PAT甲级——1108.Finding Average (20分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- 【刷题-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 1108 Finding Average (20 分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- Day 007:PAT训练--1108 Finding Average (20 分)
话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...
- 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分)
题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...
- PAT (Advanced Level) 1108. Finding Average (20)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1108. Finding Average (20)-字符串处理
求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...
- PAT 甲级 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
随机推荐
- SSH连接时,长时间不操作就断开的解觉办法
1.第一次尝试失败 修改/etc/ssh/sshd_config文件, 找到 ClientAliveInterval 0 ClientAliveCountMax 3 并将注释符号("#&qu ...
- Windows中区位码转换为机内码
步骤的简化 区位码的区和位分别加上32得到国标码, 再分别加上128得到机内码 区位码的区和位分别加上160得到机内码 区位码加上A0A0H = 机内码 示例: 我 我的区位码是4650 区位码 46 ...
- Java——方法的重写(覆盖)
2.2方法的重写(覆盖)(override,orverwrite) 2.2.1 什么时候方法要进行重写? 如果父类中的方法已经无法满足当前子类的业务需求,需要将父类中的方法进行重新写一遍.就是要改变父 ...
- Openssl命令的使用
用途: pkcs8格式的私钥转换工具.它处理在PKCS#8格式中的私钥文件.它可以用多样的PKCS#5 (v1.5 and v2.0)和 PKCS#12算法来处理没有解密的PKCS#8 Private ...
- iOS 点击Application icon加载推送通知Data
今天做APNS远程推送通知遇到了一个问题,就是手机接收到通知的时候,如果马上点击通知的 alert view时候,系统马上唤醒你的Application,通知或调用你的didReceiveLocalN ...
- NSDateFormatter 今年日期格式化成字符串是明年日期问题?
在项目里我要是把NSDate格式化成字符串 我的format是@"YYYY年MM月dd日 HH:mm" 传入日期2013-12-30 15:00:00后,返回给我的字符串是 201 ...
- 4 丑数 Ⅱ-找出第n个丑数
原题网址:http://www.lintcode.com/zh-cn/problem/ugly-number-ii/ 设计一个算法,找出只含素因子2,3,5 的第 n 小的数. 符合条件的数如:1, ...
- 漏洞验证系列--MongoDB未授权访问
本系列文章旨在对于有一定网络安全基础的人员,在日常工作中扫描出来的各种漏洞,如何进行验证,以区分该漏洞是否存在或是扫描器误报.请勿应用非法途径. 本漏洞是由于MongoDB未设置访问权限,用户可以直接 ...
- python读取Excel表格文件
python读取Excel表格文件,例如获取这个文件的数据 python读取Excel表格文件,需要如下步骤: 1.安装Excel读取数据的库-----xlrd 直接pip install xlrd安 ...
- 更改idea启动内存信息
1.到idea的安装目录的bin下,找idea64.exe.vmoptions 文件 2.更改参数 对应的参数解释: -Xms1024m 设置IDEA初时的内存大小,提高Java程序的启动速度. ...