PAT-Advanced-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:

Sample Output 1:

Sample Input 2:

Sample Output 2:

今天中午和下午抽空把 PAT-Advanced 新增的题都做掉了,感觉难度都好一般。但是自己最近的码力确实是下降了,容易犯一些低级的错误,思路也不够清晰了。

这道题需要注意的是最多精确到 2 位,同时只有 0 个或 1 个合法数字时的输出。(似乎和测试点 3 有关

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.

其他的就没有什么问题,就是发现自己的思路好不清晰,写第一遍时没有 AC ,然后偷懒直接 sscanf 反而过了……

本来打算写一个基于正则表达式的,但不知 regex.h 能不能用,回来有空尝试一下。

代码如下:

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注