pat 1108 Finding Average(20 分)

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 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:

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
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <string>
 6 #include <map>
 7 #include <stack>
 8 #include <vector>
 9 #include <queue>
10 #include <set>
11 #define LL long long
12 #define INF 0x3f3f3f3f
13 using namespace std;
14 const int MAX = 1e3;
15
16 int n, cnt = 0;
17 double ans = 0.0;
18 char s[MAX], notlegal[105][MAX];
19
20 bool my_is_ans()
21 {
22     int len = strlen(s), my_cnt = 0;
23 //    if (s[0] == ‘.‘ || s[len - 1] == ‘.‘) return false;
24     for (int i = 0; i < len; ++ i)
25     {
26         if (isalpha(s[i])) return false;
27         if (s[i] == ‘.‘)
28         {
29             my_cnt ++;
30             if (my_cnt >= 2) return false;
31             if (i <= len - 4) return false;
32         }
33     }
34     return true;
35 }
36
37 int main()
38 {
39 //    freopen("Date1.txt", "r", stdin);
40     scanf("%d", &n);
41     for (int i = 1; i <= n; ++ i)
42     {
43         scanf("%s", &s);
44         double d;
45
46         if (!my_is_ans() || sscanf(s, "%lf", &d)!= 1 || d < -1000 || d > 1000)
47         {
48             strcpy(notlegal[cnt ++], s);
49             continue;
50         }
51         ans += d;
52     }
53
54     for (int i = 0; i < cnt; ++ i)
55         printf("ERROR: %s is not a legal number\n", notlegal[i]);
56     if (n - cnt == 0)
57         printf("The average of 0 numbers is Undefined\n");
58     else if (n - cnt == 1)
59         printf("The average of 1 number is %.2lf\n", ans);
60     else
61         printf("The average of %d numbers is %.2lf\n", n - cnt, ans / (n - cnt));
62     return 0;
63 }

原文地址:https://www.cnblogs.com/GetcharZp/p/9593742.html

时间: 2024-10-02 23:56:46

pat 1108 Finding Average(20 分)的相关文章

PAT 甲级 1108 Finding Average (20分)

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 of the input numbers might not be legal. A legal input is a real number in [−] and is a

PAT甲题题解-1108. Finding Average (20)-字符串处理

求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=105; bool islegal(char*str){ int len=strlen(str); int p

PAT 1108 Finding Average

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 m

1108 Finding Average (20 分)

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 of the input numbers might not be legal. A legal input is a real number in [−1000,1000

pat 1035 Password(20 分)

1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) fro

pat 1077 Kuchiguse(20 分) (字典树)

1077 Kuchiguse(20 分) The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often

[PTA] PAT(A) 1008 Elevator (20 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Problem portal: 1008 Elevator (20 分) Description  The highest building in our city has only one elevator. A request list is made up with $N$ positive numbers

PAT乙级1088-----三人行 (20分)

1088 三人行 (20分) 输入样例 1: 48 3 7 输出样例 1: 48 Ping Cong Gai 输入样例 2: 48 11 6 输出样例 2: No Solution 思路:1.丙的能力值有可能是小数因此要用double 首次通过代码: 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 5 6 7 int main(){ 8 int m,x,y; 9 int flag=1; 10 s

PAT甲级——1035 Password (20分)

To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase)