PAT Advanced 1108 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 <cstring>
using namespace std;
int main()
{
int N;cin>>N;
double avg=;
int val,coun=;
double a;
while(N--){
bool right=true;
char tmp[],tmp2[];
scanf("%s",tmp);
sscanf(tmp,"%lf",&a);
sprintf(tmp2,"%.2f",a);
for(int i=;i<strlen(tmp);i++)
if(tmp[i]!=tmp2[i]) right=false;
if(right&&a<=&&a>=-) {
avg+=a;
coun++;
}
else printf("ERROR: %s is not a legal number\n",tmp);
}
if(coun!=) {
if(coun>){
avg/=coun;
printf("The average of %d numbers is %.2f",coun,avg);
}else printf("The average of 1 number is %.2f",avg);
}else printf("The average of 0 numbers is Undefined"); system("pause");
return ;
}
PAT Advanced 1108 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 ...
- Day 007:PAT训练--1108 Finding Average (20 分)
话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...
- 【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 Advanced 1152 Google Recruitment (20 分)
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...
- PAT Advanced 1042 Shuffling Machine (20 分)(知识点:利用sstream进行转换int和string)
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...
- PAT Advanced 1140 Look-and-say Sequence (20 分)
Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...
- PAT Advanced 1073 Scientific Notation (20 分)
Scientific notation is the way that scientists easily handle very large numbers or very small number ...
随机推荐
- Maven下载依赖包所使用的方法或者说三方包
wagon-http-3.2.0-shaded.jar 下载主要用的是这个包,mac位于路径/usr/local/Cellar/maven/3.6.0/libexec/lib下 如图,即使修改jar包 ...
- SMOS数据产品介绍与下载方法
1. SMOS数据介绍 The Soil Moisture and Ocean Salinity (SMOS) 卫星是欧空局发射的一颗以探测地球土壤水含量以及海表盐度为目标的卫星,卫星所搭载的唯一载荷 ...
- opencv轮廓外接矩形
1.寻找轮廓 api void cv::findContours( InputOutputArray image, OutputArrayOfArrays contours, OutputArray ...
- ${__setProperty(row,rowNum)};不能在import XXX后面使用;
如下 ${__javaScript只能用一次调用 excel.CWResultFile.CWOutputFile.wOutputFile("/Users/iot/1.xls", & ...
- 【LOJ】#3020. 「CQOI2017」小 Q 的表格
#3020. 「CQOI2017」小 Q 的表格 这个的话求出来\(g = gcd(a,b)\) 会修改所有gcd为g的位置 我们要求\((g,g)\)这个位置的数一定是\(g^{2}\)的倍数 之后 ...
- SecureCRT SSH 失败 Key exchange failed 解决方法
背景:SecureCRT 的SSH正常使用过程中,突然出现: Key exchange failed. No compatible hostkey.The server supports these ...
- Scratch第四十九讲:完美的下落和反弹
做了很多小游戏,都会遇到碰撞和反弹的情况,CC哥大多时候也都是简单处理一下,包括之前的讲座也有提过,但是没有认真的讲解过.今天就专门为这个主题做一讲,把这部分内容彻底讲透,大家可以一起探讨一下. 是不 ...
- 项目element-ui checkbox里面获取选中项 实现批量删除 修改
<el-table :data="tableData" stripe border style="width: 100%" @selection-chan ...
- 导入别的项目到我的eclipse上出现红色感叹号问题
项目红色感叹号问题问题 一般我们在导入别的项目到我的eclipse上面会发现,项目上面有红色的错误 原因 因为我电脑上的 jdk版本和别人电脑jdk版本不一样,那么对于的jre版本也不 ...
- 在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组)
原文:在论坛中出现的比较难的sql问题:8(递归问题 树形结构分组) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必 ...