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;
}

最新文章

  1. 从网易与淘宝的font-size思考前端设计稿与工作流
  2. linux+php+apache web调用python脚本权限问题
  3. dede自定义表单增加添加时间怎么弄
  4. Android基本认识
  5. Codeforces Round #233 (Div. 2) A、Pages
  6. 洛谷P2085 最小函数值(minval)
  7. bss段为什么要初始化,清除
  8. Linux中Firefox——Httpfox插件安装及使用
  9. .NET/Mono
  10. (转)如何向map和reduce脚本传递参数
  11. 用PE安装操作系统时:无法创建新的系统分区 也无法定位现有系统分区 的解决办法
  12. 疑难杂症:Java中Scanner连续获取int和String型发生错误.
  13. 【API知识】RestTemplate的使用
  14. 易宝支付Demo,生产中封装成简洁的代付接口,不用request如何获取项目运行时的真实路径
  15. 类自动调用to.string方法
  16. substr、substring和slice的区别
  17. 最小k度限制生成树
  18. 二叉搜索树、AVL平衡二叉搜索树、红黑树、多路查找树
  19. 了解Flask 信号机制
  20. 关于设置服务器为https服务器

热门文章

  1. apache ua Custom Log Formats
  2. RESTful设计原则和样例(开发前后台接口)
  3. POJ 2195 Going Home 最小费用流
  4. Java - TCP网络编程
  5. PCB 内层负片散热PAD Symbols尺寸更改方法
  6. jquery中对于为一组标签赋予点击事件
  7. StreamingListener技术点
  8. Android高级开发-布局渲染流程与优化
  9. C# System.Environment.GetFolderPath的使用 [转]
  10. MAC应用无法打开或文件损坏的处理方法