首页
学习
活动
专区
圈层
工具
发布
30 篇文章
1
PAT (Basic Level) Practice (中文)1054 求平均值 (20 分)
2
PAT (Basic Level) Practice (中文)1051 复数乘法 (15 分)
3
PAT (Basic Level) Practice (中文)1050 螺旋矩阵 (25 分)
4
PAT (Basic Level) Practice (中文)1046 划拳 (15 分)
5
PAT (Basic Level) Practice (中文)1045 快速排序 (25 分)
6
PAT (Basic Level) Practice (中文)1047 编程团体赛 (20 分)
7
PAT (Basic Level) Practice (中文)1043 输出PATest (20 分)
8
PAT (Basic Level) Practice (中文)1042 字符统计 (20 分)
9
PAT (Basic Level) Practice (中文)1040 有几个PAT (25 分)
10
PAT (Basic Level) Practice (中文)1038 统计同成绩学生 (20 分)
11
PAT (Basic Level) Practice (中文)1036 跟奥巴马一起编程 (15 分)
12
PAT (Basic Level) Practice (中文)1034 有理数四则运算 (20 分)
13
PAT (Basic Level) Practice (中文)1032 挖掘机技术哪家强 (20 分)
14
PAT (Basic Level) Practice (中文)1031 查验身份证 (15 分)
15
PAT (Basic Level) Practice (中文)1030 完美数列 (25 分)
16
PAT (Basic Level) Practice (中文)1029 旧键盘 (20 分)
17
PAT (Basic Level) Practice (中文)1028 人口普查 (20 分)
18
PAT (Basic Level) Practice (中文)1027 打印沙漏 (20 分)
19
PAT (Basic Level) Practice (中文)1025 反转链表 (25 分)
20
PAT (Basic Level) Practice (中文)1053 住房空置率 (20 分)
21
PAT (Basic Level) Practice (中文)1024 科学计数法 (20 分)
22
PAT (Basic Level) Practice (中文)1022 D进制的A+B (20 分)
23
PAT (Basic Level) Practice (中文)1049 数列的片段和 (20 分)
24
PAT (Basic Level) Practice (中文)1021 个位数统计 (15 分)
25
PAT (Basic Level) Practice (中文)1048 数字加密 (20 分)
26
PAT (Basic Level) Practice (中文)1019 数字黑洞 (20 分)
27
PAT (Basic Level) Practice (中文)1017 A除以B (20 分)
28
PAT (Basic Level) Practice (中文)1016 部分A+B (15 分)
29
PAT (Basic Level) Practice (中文)1015 德才论 (25 分)
30
PAT (Basic Level) Practice (中文)1014 福尔摩斯的约会 (20 分)

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:

代码语言:javascript
复制
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

输出样例 1:

代码语言:javascript
复制
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:

代码语言:javascript
复制
2
aaa -9999

输出样例 2:

代码语言:javascript
复制
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined

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

代码语言:javascript
复制
#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

代码语言:javascript
复制
// 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;
}
下一篇
举报
领券