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 ofthe input numbers might not be legal. A "legal" input is a real numberin [-1000, 1000] and is accurate up to no more than 2 decimal places.When you calculate the average, those illegal numbers must not becounted 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 thenext 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 theresult: "The average of K numbers is Y" where K is the number of legalinputs and Y is their average, accurate to 2 decimal places. In casethe average cannot be calculated, output "Undefined" instead of Y. Incase K is only 1, output "The average of 1 number is Y" instead.
这道题本来挺简单的,但是要求检查输入格式。看了一下通过率还是挺惨的。
这种题第一个想到的是正则,然而不知道为什么使用正则会导致编译超时。?_ ?? 既然这样那就手工构造DFA,效率高的可怕。还有一个办法就是利用sprintf讲读入的浮点数再比较一下。sscanf(a, "%lf", &f);sprintf(b, "%.2lf",f);
#include#include #include #include #include #include #include #include