PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)

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

1012 数字分类 (20 分)

给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:

  • A​1​​ = 能被 5 整除的数字中所有偶数的和;
  • A​2​​ = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n​1​​−n​2​​+n​3​​−n​4​​⋯;
  • A​3​​ = 被 5 除后余 2 的数字的个数;
  • A​4​​ = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位;
  • A​5​​ = 被 5 除后余 4 的数字中最大数字。

输入格式:

每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。

输出格式:

对给定的 N 个正整数,按题目要求计算 A​1​​~A​5​​ 并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出 N

输入样例 1:

13 1 2 3 4 5 6 7 8 9 10 20 16 18

输出样例 1:

30 11 2 9.7 9

输入样例 2:

8 1 2 4 5 6 7 9 16

输出样例 2:

N 11 2 N 9

有点繁琐 要注意最后一个数字不能带空格输出不存在输出N

// 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 t,cnt=1,cntt;
ll a[10],flag[10];
int main()
{
    cin>>t;
    a[5]=-inf;
    for(rg i=1;i<=t;i++)
    {
       ll x=read();
       if(x%5==0&&x%2==0)a[1]+=x,flag[1]=1;
       if(x%5==1)
       {
           if(cnt%2)
           {
               a[2]+=x;
           }
           else a[2]-=x;
           cnt++;
           flag[2]=1;
       }
       if(x%5==2)a[3]++,flag[3]=1;
       if(x%5==3)a[4]+=x,cntt++,flag[4]=1;
       if(x%5==4)a[5]=max(a[5],x),flag[5]=1;
    }

    for(rg i=1;i<=5;i++)
    {
        if(i==4&&flag[4]==0)
        {
            cout<<"N"<<" ";
            continue;
        }
        if(i==4&&flag[i])
        {
            cout<<setiosflags(ios::fixed)<<setprecision(1)<<a[i]*1.0/(1.0*cntt)<<" ";
            continue;
        }
        if(flag[i]&&i!=5)
        {
            cout<<a[i]<<" ";
        }
        else if(flag[i]==0&&i!=5)
        {
            cout<<"N"<<" ";
        }
        else if(flag[i]&&i==5)
        {
            cout<<a[i]<<endl;
        }
        else if(flag[i]==0&&i==5)
        {
            cout<<"N"<<endl;
        }
    }
   	return 0;
}