PAT_A1108#Finding Average

Source:

PAT A 1108 Finding Average (20 分)

Description:

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 [−] 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 (≤). 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:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

Sample Output 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

Sample Input 2:

2
aaa -9999

Sample Output 2:

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

Keys:

  • 字符串处理
  • string(C++ STL)

Attention:

  • 小数先相加再乘基数,整数先乘基数再相加
  • str.c_str()函数
  • s.size()是unsigned,if(s.size()-pos-1 > 2)这个操作,实际上是无符号整数比较大小,真值的负数会大于正数

Code:

 1 /*
 2 Data: 2019-06-16 13:51:57
 3 Problem: PAT_A1108#Finding Average
 4 AC: 24:26
 5 
 6 题目大意:
 7 计算n个数的平均值,合法数范围在[-1000,1000],且不超过两位小数
 8 */
 9 
10 #include<cstdio>
11 #include<string>
12 #include<iostream>
13 using namespace std;
14 
15 bool isLegal(string s, double &num)
16 {
17     int sign=1;
18     if(s[0] == '-')
19     {
20         sign=-1;
21         s.erase(0,1);
22     }
23     int pos=s.size();
24     for(int i=0; i<s.size(); i++)
25     {
26         if(s[i] == '.')
27         {
28             if(pos==s.size())
29                 pos = i;
30             else
31                 return false;
32         }
33         else if(s[i]<'0' || s[i]>'9')
34             return false;
35     }
36     int len = s.size()-pos-1;
37     if(len > 2)
38         return false;
39     num=0;
40     double upper=0,lower=0;
41     for(int i=0; i<pos; i++)
42     {
43         upper *= 10;
44         upper += (s[i]-'0');
45     }
46     for(int i=s.size()-1; i>pos; i--)
47     {
48         lower += (s[i]-'0');
49         lower *= 0.1;
50     }
51     num = upper + lower;
52     if(num > 1000)
53         return false;
54     num *= sign;
55     return true;
56 }
57 
58 int main()
59 {
60 #ifdef  ONLINE_JUDGE
61 #else
62     freopen("Test.txt", "r", stdin);
63 #endif // ONLINE_JUDGE
64 
65     int n,cnt=0;
66     string str;
67     double sum=0,num;
68     scanf("%d", &n);
69     for(int i=0; i<n; i++)
70     {
71         cin >> str;
72         if(isLegal(str,num))
73         {
74             cnt++;
75             sum += num;
76         }
77         else
78             printf("ERROR: %s is not a legal number\n", str.c_str());
79     }
80     if(cnt==0)
81         printf("The average of 0 numbers is Undefined");
82     else if(cnt==1)
83         printf("The average of 1 number is %.2f", sum);
84     else
85         printf("The average of %d numbers is %.2f", cnt,sum/cnt);
86 
87     return 0;
88 }

Update:

 1 /*
 2 Data: 2019-08-17 20:23:41
 3 Problem: PAT_A1108#Finding Average
 4 AC: 24:26
 5 
 6 题目大意:
 7 计算n个数的平均值,合法数范围在[-1000,1000],且不超过两位小数
 8 */
 9 #include<cstdio>
10 #include<string>
11 #include<iostream>
12 using namespace std;
13 const int Er=1e4;
14 
15 double ToF(string s)
16 {
17     int cnt=0,acr=0;
18     string x="";
19     for(int i=0; i<s.size(); i++)
20     {
21         x += s[i];
22         if(s[i]=='-' && i==0)
23             continue;
24         if(cnt)
25             acr++;
26         if(s[i] == '.')
27         {
28             cnt++;
29             if(cnt==2)
30                 return Er;
31         }
32         else if(s[i]<'0' || s[i]>'9')
33             return Er;
34     }
35     if(acr>2)
36         return Er;
37     else
38         return atof(x.c_str());
39 }
40 
41 int main()
42 {
43 #ifdef ONLINE_JUDGE
44 #else
45     freopen("Test.txt", "r", stdin);
46 #endif // ONLINE_JUDGE
47 
48     int n,cnt=0;
49     double x=0,sum=0;
50     string s;
51     scanf("%d", &n);
52     for(int i=0; i<n; i++)
53     {
54         cin >> s;
55         x = ToF(s);
56         if(x>1e3 || x<-1e3)
57             printf("ERROR: %s is not a legal number\n", s.c_str());
58         else
59         {
60             sum += x;
61             cnt++;
62         }
63     }
64     if(cnt==0)
65         printf("The average of 0 numbers is Undefined\n");
66     else if(cnt==1)
67         printf("The average of 1 number is %.2f\n", sum);
68     else
69         printf("The average of %d numbers is %.2f\n", cnt,sum/cnt);
70 
71     return 0;
72 }

 

 

 

posted @ 2019-06-16 15:35  林東雨  阅读(264)  评论(0编辑  收藏  举报