PAT (Basic Level) Practice (中文)1054 求平均值 (20 分)

时间:2022-07-26
本文章向大家介绍PAT (Basic Level) Practice (中文)1054 求平均值 (20 分),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1054 求平均值 (20 分)

本题的基本要求非常简单:给定 N 个实数,计算它们的平均值。但复杂的是有些输入数据可能是非法的。一个“合法”的输入是 [−1000,1000] 区间内的实数,并且最多精确到小数点后 2 位。当你计算平均值的时候,不能把那些非法的数据算在内。

输入格式:

输入第一行给出正整数 N(≤100)。随后一行给出 N 个实数,数字间以一个空格分隔。

输出格式:

对每个非法输入,在一行中输出 ERROR: X is not a legal number,其中 X 是输入。最后在一行中输出结果:The average of K numbers is Y,其中 K 是合法输入的个数,Y 是它们的平均值,精确到小数点后 2 位。如果平均值无法计算,则用 Undefined 替换 Y。如果 K 为 1,则输出 The average of 1 number is Y

输入样例 1:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

输出样例 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

输入样例 2:

2
aaa -9999

输出样例 2:

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

这道题的难点在于非法字符串的判断,关于这个我们引入两个工具函数ssanf和sprintf ,介绍如下

#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
    
    //--------------数字转字符串,使用sprintf(char数组名,类型,数字变量名) 
    //整数转十进制字符串
    char str[10];
    int a=1234321;
    sprintf(str,"%d",a);
    //str="1234321";
    //小数转十进制字符串 
    char str1[10];
    double b=123.321;
    sprintf(str1,"%.3lf",b);
    str1="123.321";
    //十进制整数转十六进制字符串
    char str2[10];
    int c=175;
    sprintf(str2,"%x",c);
    cout<<"数字-->字符串:"<<endl;
    cout<<str<<endl<<str1<<endl<<str2<<endl;
    //str2="af";
    //----------------字符串转数字,使用sscanf()函数
    //字符串转整数 
    char str3[]="1234321";
    int d;
    sscanf(str3,"%d",&d); 
    //d=123;
    //字符串转小数
    char str4[]="123.321";
    double e;
    sscanf(str4,"%lf",&e);
    //十六进制字符串转十进制整数
    char str5[]="AF";
    int f;
    sscanf(str5,"%x",&f); 
    //f=175;
    cout<<"字符串-->数字:"<<endl;
    cout<<d<<endl<<e<<endl<<f<<endl;
}

有了这个基础知识铺垫,我们就好做这题了~

具体请看代码,唯一要注意的是当只有一个数字的时候输出的是number而不是numbers

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 300005
#define lb(x) (x&(-x))
const double eps = 1e-6;
using namespace std;
inline ll read()
{
	char ch = getchar(); ll s = 0, w = 1;
	while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
	return s * w;
}
inline void write(ll x)
{
	if (x < 0)putchar('-'), x = -x;
	if (x > 9)write(x / 10);
	putchar(x % 10 + 48);
}
ll n;
char a[105],b[105];
int main()
{
    cin>>n;
    double temp=0,sum=0;
    ll cnt=0;
    for(rg i=1;i<=n;i++)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%s",a);
        sscanf(a,"%lf",&temp);
        sprintf(b,"%.2lf",temp);
        ll flag=0;
        for(rg j=0;a[j];j++)
        {
            if(a[j]!=b[j])
            {
                flag=1;
                break;
            }
        }
        //cout<<temp<<" "<<b<<endl;
        if(flag||temp>1000||temp<-1000)
        {
            cout<<"ERROR: "<<a<<" is not a legal number"<<endl;
        }
        else cnt++,sum+=temp;
    }
    if(!cnt)
    {
        cout<<"The average of 0 numbers is Undefined"<<endl;
        return 0;
    }
    if(cnt==1)
    {
        cout<<"The average of 1 number is "<<fixed<<setprecision(2)<<sum<<endl;
    }
    else
    {
        cout<<"The average of "<<cnt<<" numbers is "<<fixed<<setprecision(2)<<sum/cnt<<endl;
    }
    return 0;
}