1108 Finding Average (20 分)
Posted mered1th
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 /**
2 * Copyright(c)
3 * All rights reserved.
4 * Author : Mered1th
5 * Date : 2019-02-26-19.44.56
6 * Description : A1108
7 */
8 #include<cstdio>
9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19
20 int main(){
21 #ifdef ONLINE_JUDGE
22 #else
23 freopen("1.txt", "r", stdin);
24 #endif
25 int n,num=0;
26 cin>>n;
27 string str;
28 double ans=0.0;
29 for(int i=0;i<n;i++){
30 cin>>str;
31 int len=str.length(),j;
32 bool flag=false;
33 for(j=0;j<len;j++){
34 if(str[j]==‘-‘) continue;
35 if(str[j]==‘.‘&&flag==false){
36 flag=true;
37 if(len-j-1>2){
38 printf("ERROR: %s is not a legal number
",str.c_str());
39 break;
40 }
41 }
42 else if((str[j]==‘.‘ &&flag==true)||(!(str[j]>=‘0‘ && str[j]<=‘9‘))){
43 printf("ERROR: %s is not a legal number
",str.c_str());
44 break;
45 }
46 }
47 if(j==len){
48 double temp=stod(str);
49 if(temp>=-1000 &&temp<=1000){
50 ans+=temp;
51 num++;
52 }
53 else{
54 printf("ERROR: %s is not a legal number
",str.c_str());
55 }
56 }
57 }
58 if(num>1){
59 printf("The average of %d numbers is %.2f
",num,ans/num);
60 }
61 else if(num==0){
62 printf("The average of 0 numbers is Undefined
");
63 }
64 else if(num==1){
65 printf("The average of %d number is %.2f
",num,ans/num);
66 }
67 return 0;
68 }
以上是关于1108 Finding Average (20 分)的主要内容,如果未能解决你的问题,请参考以下文章
PAT 甲级 1108 Finding Average (20分)
1108 Finding Average (20 分)难度: 简单 / 模拟
PAT甲题题解-1108. Finding Average (20)-字符串处理