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

分析:用非常好用的sscanf和sprintf即可解决~
sscanf() – 从一个字符串中读进与指定格式相符的数据
sprintf() – 字符串格式化命令,主要功能是把格式化的数据写入某个字符串中

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main() {int n, cnt = 0;char a[50], b[50];double temp = 0.0, sum = 0.0;cin >> n;for(int i = 0; i < n; i++) {scanf("%s", a);sscanf(a, "%lf", &temp);sprintf(b, "%.2f",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 %.2f", sum);else if(cnt > 1)printf("The average of %d numbers is %.2f", cnt, sum / cnt);elseprintf("The average of 0 numbers is Undefined");return 0;
}

1108. Finding Average (20)-PAT甲级真题相关推荐

  1. 1120. Friend Numbers (20)-PAT甲级真题

    1120. Friend Numbers (20) Two integers are called "friend numbers" if they share the same ...

  2. 1042. Shuffling Machine (20)-PAT甲级真题

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  3. 1081. Rational Sum (20)-PAT甲级真题

    Given N rational numbers in the form "numerator/denominator", you are supposed to calculat ...

  4. 1077. Kuchiguse (20)-PAT甲级真题

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  5. 1031. Hello World for U (20)-PAT甲级真题

    Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...

  6. 1061. Dating (20)-PAT甲级真题

    Sherlock Holmes received a note with some strange strings: "Let's date! 3485djDkxh4hhGE 2984akD ...

  7. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

  8. PAT甲级真题 1018 A+B in Hogwarts--python解法

    PAT甲级真题 1018 A+B in Hogwarts 提交:2638 通过:1559 通过率:59% If you are a fan of Harry Potter, you would kno ...

  9. PAT 甲级 1108 Finding Average (20 分)

    思路: 1.不是数字的位置只能在第一个且为负号,或者在字符串的倒数第一.二.三位置.或者不存在: 2.上面的方法也可以用sscanf或者sprintf进行判定: 3.数字要在规定区间内: 4.按.2f ...

最新文章

  1. 【CV】吴恩达机器学习课程笔记第11章
  2. stm32f302实现斩波控制步进电机_什么是步进电机控制器?
  3. 使用Django和Python创建Json response
  4. CSS浮动和清除浮动
  5. 运行时间_如何测量嵌入式软件运行时间?
  6. 如何用python画长方形_Python 画矩形
  7. Linux后门入侵检测工具 rkhunter 安装使用
  8. iso12233测试方法_ISO12233图像分辨率测试卡的使用方法
  9. php++erp,php云ERP进销存V8标准版
  10. Qt 二维码文件传输工具
  11. 电池SOC仿真系列-基于RNN的电池SOC估算研究
  12. LeetCode | 665. Non-decreasing Array
  13. php调试技术手册读书笔记,RT-Thread读书笔记之一:开始学习了
  14. verilog乘法器以及booth编码改进
  15. 用户行为分析面面观(之二)-----特征2:流动性
  16. 大型企业如何搭建私有云计算平台
  17. 软件工程学习笔记(七)~编写高质量代码
  18. c#实现文件重命名操作
  19. 【Codecs系列】mpeg2解码器开源实现:libmpeg2
  20. 虚拟空间phpMyAdmin配置

热门文章

  1. Druid Monitor监控Java Web和Java SE项目
  2. 用nodejs做一个svn密码修改页面
  3. Hibernate 对c3p0配置不支持导致的错误
  4. poj2586 Y2K Accounting Bug(贪心)
  5. 视频:网站建设-如何利用网络赚大钱2
  6. 利用中继攻击解锁并开走汽车,本田不打算修复(含视频)
  7. Node.js 修复4个漏洞
  8. 专家从美国联邦政府机构网络上发现后门
  9. Drupal 修复远程代码执行漏洞
  10. 电商巨头Shopify 两名员工被指窃取客户交易详情