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

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

题意:

给出一组数据,判断输出是不是合法数字(精度不超过小数点后两位,范围【-1000,1000】),有合法数据还要累加算出average;不存在合法数据输出The average of 0 numbers is Undefined

题解:

第一发没过原因是number和numbers没有看清楚。k=0,1,>1要分开讨论。

注意细节:详见代码注释。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    int k=0;
    double sum=0;
    string a;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a;
        double add=0;
        int l=a.length();
        int fu=0;
        int p=-1;
        int legal=1;
        for(int i=0;i<l;i++){
            if(i==0){
                if(a[i]==‘-‘){
                    fu=1;
                    continue;
                }
            }
            if(a[i]==‘.‘){
                if(p!=-1){//记录小数点位置
                    legal=0;
                    break;
                }else{
                    p=i;
                    if(l-i>3){//小数点后有三位了,不合法
                        legal=0;
                        break;
                    }
                }
            }else if(a[i]>=‘0‘&&a[i]<=‘9‘){
                if(p!=-1&&i-p==1){//小数点后两位特殊处理
                    add=add+(a[i]-‘0‘)*0.1;
                }else if(p!=-1&&i-p==2){
                    add=add+(a[i]-‘0‘)*0.01;
                }else{
                    add=add*10+a[i]-‘0‘;
                }
                if(abs(add)>1000){//数过大或过小
                    legal=0;
                    break;
                }
            }else{
                legal=0;
                break;
            }
        }
        if(!legal){
            printf("ERROR: %s is not a legal number\n",a.c_str());
        }else{
            if(fu==1) add=0-add;//负数
            sum+=add;
            k++;
        }
    }
    if(k==0){
        printf("The average of 0 numbers is Undefined");
    }else if(k==1){
        printf("The average of %d number is %.2lf",k,sum/k);
    }else{
        printf("The average of %d numbers is %.2lf",k,sum/k);
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/caiyishuai/p/12258029.html

时间: 2024-11-05 17:24:42

PAT 甲级 1108 Finding Average (20分)的相关文章

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1]. The first one who bets on a unique number wins. For example

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甲级】1008 Elevator (20 分)

题意: 电梯初始状态停在第0层,给出电梯要接人的层数和层序号,计算接到所有人需要的时间,接完人后电梯无需回到1层(1层不是0层).电梯上升一层需要6秒,下降一层需要4秒,接人停留时间为5秒. 代码: #include<bits/stdc++.h> using namespace std; int a[100007]; int main(){ int n; cin>>n; int ans=0; for(int i=1;i<=n;++i){ cin>>a[i]; if

【PAT甲级】1077 Kuchiguse (20 分)(cin.ignore()吃掉输入n以后的回车接着用getine(cin,s[i])输入N行字符串)

题意: 输入一个正整数N(<=100),接着输入N行字符串.输出N行字符串的最长公共后缀,否则输出nai. 代码: #include<bits/stdc++.h>using namespace std;string s[107];int length[107];char ans[307];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; cin.igno

【PAT甲级】1008 Elevator (20分)

1008 Elevator 题目: The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one

PAT甲级——1073 Scientific Notation (20分)

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9].[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at lea

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]

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