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 /**
 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\n",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\n",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\n",str.c_str());
55             }
56         }
57     }
58     if(num>1){
59         printf("The average of %d numbers is %.2f\n",num,ans/num);
60     }
61     else if(num==0){
62         printf("The average of 0 numbers is Undefined\n");
63     }
64     else if(num==1){
65         printf("The average of %d number is %.2f\n",num,ans/num);
66     }
67     return 0;
68 }
 

原文地址:https://www.cnblogs.com/Mered1th/p/10441171.html

时间: 2024-08-29 19:58:44

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

6-2 顺序表操作集(20 分)

6-2 顺序表操作集(20 分) 本题要求实现顺序表的操作集. 函数接口定义: List MakeEmpty(); Position Find( List L, ElementType X ); bool Insert( List L, ElementType X, Position P ); bool Delete( List L, Position P ); 其中List结构定义如下: typedef int Position; typedef struct LNode *List; str

pat 1136 A Delayed Palindrome(20 分)

1136 A Delayed Palindrome(20 分) Consider a positive integer N written in standard notation with k+1 digits a?i?? as a?k???a?1??a?0?? with 0≤a?i??<10 for all i and a?k??>0. Then N is palindromic if and only if a?i??=a?k?i?? for all i. Zero is written

1136 A Delayed Palindrome (20 分)

1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k+1 digits a?i?? as a?k???a?1??a?0?? with 0 for all iand a?k??>0. Then N is palindromic if and only if a?i??=a?k−i?? for all i. Zero is written 0 and is

PTA乙级 (*1054 求平均值 (20分))

1054 求平均值 (20分) https://pintia.cn/problem-sets/994805260223102976/problems/994805272659214336 #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <cmath> #include <algorithm> #include <

PTA-C-4-7 统计某类完全平方数 (20分)

4-7 统计某类完全平方数   (20分) 本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144.676等. 函数接口定义: int IsTheNumber ( const int N ); 其中N是用户传入的参数.如果N满足条件,则该函数必须返回1,否则返回0. 裁判测试程序样例: #include <stdio.h> #include <math.h> int IsTheNumber ( const int N ); int ma