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:
1 2 |
7 5 -3.2 aaa 9999 2.3.4 7.123 2.35 |
Sample Output 1:
1 2 3 4 5 |
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:
1 2 |
2 aaa -9999 |
Sample Output 2:
1 2 3 |
ERROR: aaa is not a legal number ERROR: -9999 is not a legal number The average of 0 numbers is Undefined |
今天中午和下午抽空把 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 能不能用,回来有空尝试一下。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
#include<stdio.h> #include<stdlib.h> #define DOT 1 #define SIGN 2 #define INT 4 #define FLOAT 8 #define LARGE 16 int check(char *buf) { int flag=0,i,count,temp; char dot; for(i=0;buf[i];i++) { switch(buf[i]) { case '.': if(flag&DOT) { return 0; } else { flag=flag|DOT; } break; case '+': case '-': if(flag&SIGN||flag&DOT) { return 0; } else { flag=flag|SIGN; } break; default: if(!(buf[i]>='0'&&buf[i]<='9')) { return 0; } } } for(i=0;buf[i]!='.'&&buf[i]!=0;i++) { ; } count=0; for(i;buf[i];i++) { count++; } if(count>3) { return 0; } return 1; /* for(i=0;buf[i]!='.'&&buf[i]!=0;i++)//之前写的测试过程 { ; } dot=buf[i]; buf[i]=0; sscanf(buf,"%d",&temp); buf[i]=dot; if(temp>1000||temp<-1000) { return 0; } if(buf[i]==0) { return 1; } if(temp==1000||temp==-1000) { flag=flag|LARGE; } temp=0; buf[i]='0'; sscanf(buf+i,"%d",&temp); buf[i]=dot; count=0; for(i++;buf[i];i++) { count++; } if(count>2) { return 0; } if((flag&LARGE)&&temp==0||temp<100&&(flag&LARGE)==0) { return 1; } else { return 0; }*/ } int main() { int n,i,total=0; double sum=0,temp; char buf[101]; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s",buf); if(check(buf)) { sscanf(buf,"%lf",&temp); if(temp<=1000.00&&temp>=-1000.00) { sum+=temp; total++; } else { printf("ERROR: %s is not a legal number\n",buf); } } else { printf("ERROR: %s is not a legal number\n",buf); } } if(total==0) { printf("The average of 0 numbers is Undefined\n"); } else if(total==1) { printf("The average of %d number is %.2lf\n",total,sum); } else { printf("The average of %d numbers is %.2lf\n",total,sum/total); } } |