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的更多相关文章

  1. PAT 1108 Finding Average [难]

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  2. pat 1108 Finding Average(20 分)

    1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...

  3. 1108 Finding Average (20 分)

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  4. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  5. PAT甲题题解-1108. Finding Average (20)-字符串处理

    求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...

  6. PAT Advanced 1108 Finding Average (20 分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  7. 【PAT甲级】1108 Finding Average (20分)

    题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...

  8. PAT甲级——1108.Finding Average (20分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  9. Day 007:PAT训练--1108 Finding Average (20 分)

    话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...

随机推荐

  1. ios12--简易购物车

    Assets.xcassets图片是拖到右边里面去的. // // ViewController.m // 03-综合练习 // #import "ViewController.h" ...

  2. new Modifier (C# Reference)

    https://msdn.microsoft.com/en-us/library/435f1dw2.aspx When used as a declaration modifier, the new  ...

  3. How to: Specify the Web Server for Web Projects in Visual Studio

    https://msdn.microsoft.com/en-us/library/ms178108(v=vs.120).aspx When you run a Web project in Visua ...

  4. 不仅开源,而且对企业应用完全免费!ExtAspNet弃用GPL v2,拥抱Apache License 2.0(转)

    不仅开源,而且对企业应用完全免费!ExtAspNet弃用GPL v2,拥抱Apache License 2.0(转) 提出问题 ExtAspNet开源以来,一直坚持开源免费的原则,但是其GPL v2的 ...

  5. pandas删除满足特定列信息的行记录

    #!/usr/bin/python import pandas as pd df = pd.read_excel('c:\data\zichan.xlsx') df_sn = pd.read_exce ...

  6. Hadoop - WordCount代码示例

    文章来源:http://www.itnose.net/detail/6197823.html import java.io.IOException; import java.util.Iterator ...

  7. eclipse----快速设置主题色

  8. 服务器上oracle的监听设置

    1.查看本机的计算机名 2.修改etc/host 3.修改oracle的listener.ora(我服务器上的路径:) 4.修改tnsnames.ora(和上边文件一个目录)

  9. String字符串的完美度

    题目详情: 我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同, 而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度 ...

  10. js中添加node.js语法支持

    File——>settings