PAT 1108 Finding Average
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
#include<iostream> //sscanf,sprintf的运用
#include<string.h>
using namespace std;
int main(){
char a[50], b[50];
double sum, temp;
int N, cnt=0;
cin>>N;
for(int i=0; i<N; i++){
scanf("%s",a);
sscanf(a,"%lf",&temp);
sprintf(b,"%.2lf",temp);
int flag=0;
for(int j=0; j<strlen(a); j++){
if(a[j]!=b[j])
flag=1;
}
if(flag || temp < -1000 || temp > 1000) {
printf("ERROR: %s is not a legal number\n", a);
continue;
} else {
sum += temp;
cnt++;
}
}
if(cnt == 1) {
printf("The average of 1 number is %.2lf", sum);
} else if(cnt > 1) {
printf("The average of %d numbers is %.2lf", cnt, sum / cnt);
} else {
printf("The average of 0 numbers is Undefined");
}
return 0;
}
PAT 1108 Finding Average的更多相关文章
- 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 ...
- 1108 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 ...
- PAT甲题题解-1108. Finding Average (20)-字符串处理
求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...
- PAT Advanced 1108 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甲级——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,最小:若给定个数为奇数时, ...
随机推荐
- sed 之 N D P
sed的N,D,P 是用于多行模式空间的命令,分别对应于n,d,p n & N: n(next)输出模式空间的内容,然后读取新的输入行,n命令不创建多行模式空间:N(Next)通过读取新的输入 ...
- [Codeforces Round495A] Sonya and Hotels
[题目链接] https://codeforces.com/contest/1004/problem/A [算法] 直接按题意模拟即可 时间复杂度 :O(NlogN) [代码] #include< ...
- FPC报价模块配置 UpdateCommand影响了预期 1 条记录中的 0 条 解决办法
今天在增加P4厂 FPC报价模块配置,增加刚挠信息节点,在保存时报错:UpdateCommand影响了预期 1 条记录中的 0 保存时使用:SqlDataAdapter批量更新DataTable,怎么 ...
- P2258 子矩阵(dp)
P2258 子矩阵 题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4 ...
- 北大zhw大神bzoj1756代码
#include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #i ...
- SpringBoot集成CAS单点登录,SSO单点登录,CAS单点登录(视频资料分享篇)
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很早期的公司 ...
- Python/Django 下载Excel2007
一.前提 上一篇,我写了下载Excel2003的博文,这里写下载Excel2007的博文的原因有三: 第一.Excel2003基本已经淘汰了 第二.Excel2003下载文件太大,不利于网络传输 第三 ...
- 各地图API坐标系统比较
各地图API坐标系统比较 简单记录一下各家坐标系 WGS84坐标系:即地球坐标系,国际上通用的坐标系.设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,谷歌地图采用的是WGS84地 ...
- git下
----------- 1. 分支管理策略 1)master分支 非常稳定的,只用来发布新版本,平时不在上面干活 2)dev分支 不稳定的,主要在上面干活,每个人都有自己的分支,时不时的往dev分支上 ...
- 【CodeForces727E/CF727E】Games on a CD (字符串哈希)
题目: CodeForces727E 分析: 看到字符串比较,肯定想到哈希啊--现学的哈希,先丢两个重要的公式 (\(seed\)是大于字符集大小的质数,\(p\)是大质数) \[hash[i]=(h ...