PAT 1054. 求平均值 (20)

本题的基本要求非常简单:给定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:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

输出样例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

输入样例2:

2
aaa -9999

输出样例2:

ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined

第三个测试点考虑的是当K等于1的情况

第四个测试点考虑的是[-1000,1000]边界情况,但若考虑小数点位于数最后一位是非法数,则此测试点也不能通过

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<ctype.h>
 5 #include<math.h>
 6 
 7 int main(){
 8     int n;
 9     char a[20];
10     char split[] = ".";
11     char temp1[10];
12     char temp2[10];
13     int h;
14     int h1;
15     int k;
16     double p;
17     int n1=0;
18     double sum;
19     scanf("%d",&n);
20     for(int i=0;i<n;i++){
21         scanf("%s",a);
22         k = 0;
23         h = 0;
24         for(int j=0;j<strlen(a);j++){
25             //既不是数字也不是负号和小数点,肯定是非法的。 
26             if((a[j]<'0'||a[j]>'9')&&a[j]!='-'&&a[j]!='.'){
27                 k = 1;
28                 break;
29             }
30             //是负号但负号不位于第一位
31             else if(a[j]=='-'){
32                 if(j!=0){
33                     k = 1;
34                     break;
35                 }
36             }
37             //是小数点,但小数点位于第一位,或者第一位是负号,第二位是小数点
38             //注意不要考虑最后一位是小数点的情况否则最后一个测试点不通过 
39             else if(a[j]=='.'){
40                 if(a[0]=='-'&&j==1){
41                     k = 1;
42                 }
43                 if(j==0){
44                     k = 1;
45                     break;
46                 }
47             }
48             //有两位及两位以上的小数点 
49             if(a[j]=='.'){
50                 h++;
51                 h1 = j;
52                 if(h>1){
53                     k = 1;
54                     break;
55                 }
56             }
57             
58             
59         }
60         if(k){
61             printf("ERROR: %s is not a legal number\n",a);
62         }
63         else{
64             p = atof(a);
65             if(p>1000||p<-1000)
66                 printf("ERROR: %s is not a legal number\n",a);
67             //小数点后有两位以上 
68             else if(h==1&&(strlen(a)-h1)>3){
69                 printf("ERROR: %s is not a legal number\n",a);
70             }
71             else{
72                 n1++;
73                 sum = sum+p;
74             }
75         }
76             
77     }
78     if(n1>1){
79         printf("The average of %d numbers is %.2lf",n1,sum/n1);
80     }
81     else if(n1==0)
82         printf("The average of 0 numbers is Undefined");
83     else if(n1==1)
84         printf("The average of 1 number is %.2lf",sum/n1);
85 } 

 

posted @ 2017-02-13 03:43  来一点音乐  阅读(1555)  评论(0编辑  收藏  举报