本题的基本要求非常简单:给定N个实数,计算它们的平均值。但复杂的是有些输入数据可能是非法的。一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位。当你计算平均值的时候,不能把那些非法的数据算在内。
输入格式:
输入第一行给出正整数N(<=100)。随后一行给出N个正整数,数字间以一个空格分隔。
输出格式:
对每个非法输入,在一行中输出“ERROR: X is not a legal number”,其中X是输入。最后在一行中输出结果:“The average of K numbers is Y”,其中K是合法输入的个数,Y是它们的平均值,精确到小数点后2位。如果平均值无法计算,则用“Undefined”替换Y。如果K为1,则输出“The average of 1 number is Y”。
输入样例1:
1 2 |
7 5 -3.2 aaa 9999 2.3.4 7.123 2.35 |
输出样例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 |
输入样例2:
1 2 |
2 aaa -9999 |
输出样例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-A-1108. FINDING AVERAGE 的中文版
这道题需要注意的是最多精确到 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); } } |